【发布时间】:2023-04-05 06:58:01
【问题描述】:
我只是要警告大家,我的代码在这里和那里都包含少量法语和英语,所以如果您不理解变量或其他内容,请询问并将其视为免费的法语课程哈哈。好的,所以我有一个项目,其中包括创建一个供学校使用的时间表,我负责管理“时间”部分,即为教授上课创建时间间隔和房间。为了确保所有情况都能正常工作,我需要确保两个课程不能同时在同一时间和地点发生。在我的函数“rajoutcours”(法语中的意思是“AddsClass”)中,我接受了几个参数:
- matiere = 主题
- heure_deb = 开始时间
- heure_fin = 结束时间
- date = 好吧,这是上课的日期
- sale = 房间。
在我的函数中,我创建了 3 个变量,它们是 heure_start(另一个课程开始的时间)、heure_end(另一个课程结束的时间)、day 和 room(其他课程发生的日期以及它们发生的房间) )。我使用字符串运算符“+”属性填充这些变量,在该属性中,我将 txt 文件(其他类)中的行的每个字符转换为 1 个字母字符串,以便我可以使用 sstream 库将它们相加。奇怪的是,只有一个字符被转换为字符串,而其他字符没有,我真的不明白为什么。
无论如何,我知道很多,但我一直试图找出几天,但我看不出问题出在哪里。谢谢大家的帮助。
void Utilisateur::rajout_cours(string matiere, string heure_deb, string heure_fin, string date, string salle )
{
ifstream user_in ("Cours.txt"); // txt file where classes are listed
ofstream user_out;
user_out.open("Cours.txt", ofstream::out | ofstream::app);
user_out; //write
string ligne; //line variable which reads the lines of the txt file
string heure_start; // time at which the class starts
string heure_end; // time at which the class ends
string day; // self explanatory
string room; // ie before
int i=0;
int j=0;
int cptr=0;
int a,b,c,d;
stringstream ss; // this is supposed to convert char into string
while (getline(user_in,ligne))
{
for(i=0;i<ligne.size();i++)
{
if(ligne.at(i)=='$' && cptr==0)
{
a=i;
cptr++;
cout << "Premier '$'" << endl; // Keep in mind that the '$' is the delimiter when i write on the txt file, it's also a way to know where I am.
for(j=0;j<a;j++)
{
char tmpc='\0';
string tmps;
tmpc=ligne.at(j);
ss << tmpc;
cout << "Lettre a la case " << j << " : "<< tmpc << endl; //I want to know what's the char in the j-th character of the word
ss >>tmps;
cout << "Lettre string a la case " << j << " : "<< tmps << endl; // I want to display it's string version
heure_start=heure_start+tmps;
}
}
else if(ligne.at(i)=='$' && cptr==1)
{
b=i;
cptr++;
cout << "Deuxieme '$'" << endl;
for(j=a+1;j<(b);j++)
{
char tmpc = '\0';
string tmps;
tmpc=ligne.at(j);
ss << tmpc;// conversion char en string
cout << "Lettre char a la case " << j << " : "<< tmpc << endl; //I want to know what's the char in the j-th character of the word
ss >>tmps;// conversion complète à priori
cout << "Lettre string a la case " << j << " : "<< tmps << endl; // I want to display it's string version
heure_end=heure_end+tmps;
}
}
else if(ligne.at(i)=='$' && cptr==2)
{
c=i;
cptr++;
cout << "3eme '$'" << endl;
for(j=b+1;j<(c);j++)
{
char tmpc='\0';
string tmps="";
tmpc=ligne.at(j);
ss << tmpc;
cout << "Lettre a la case " << j << " : "<< tmpc << endl; //I want to know what's the char in the j-th character of the word
ss >>tmps;
cout << "Lettre string a la case " << j << " : "<< tmps << endl; // I want to display it's string version
room=room+tmps;
}
}
else if(ligne.at(i)=='$' && cptr==3)
{
d=i;
cptr++;
cout << "4eme '$'" << endl;
for(j=c+1;j<(d);j++)
{
char tmpc='\0';
string tmps="";
tmpc=ligne.at(j);
ss << tmpc;
cout << "Lettre char a la case " << j << " : "<< tmpc << endl; //I want to know what's the char in the j-th character of the word
ss >>tmps;
cout << "Lettre string a la case " << j << " : "<< tmps << endl; // I want to display it's string version
day=day+tmps;
}
}
}
}
if(heure_deb==heure_start && heure_fin==heure_end && date==day && salle==room) // I make sure here that the class I'm writing isn't already written in the file and in that case we leave the program.
{
cout << "Impossible d'ajouter un cours ! Un cours de " << matiere <<"a deja lieu à ce moment! Changez d'horaires ou de salles. " << endl;
exit(1);
}
cout <<"ecris" << endl;
user_out << heure_deb << "$"<< heure_fin << "$" << salle << "$" << date << "$" << matiere << endl; // If not, write the new class.
}
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读the help pages,获取the SO tour,了解how to ask good questions,以及this question checklist。最后学习如何创建Minimal, Complete, and Verifiable Example。
-
如前所述,请缩小您的代码以重现相关错误。这被认为是minimal reproducible example 并且是您的问题所必需的on-topic。
-
顺便说一句。有一种非常简单的方法可以将
char转换为std::string,因为后者有一个专用的构造函数:std::string::string(size_t count, char ch)。由于count参数,您可能忽略了它。 (我个人认为,引入它不仅是为了方便,也是为了消除许多构造函数的歧义。)但是,它是这样工作的:char c = 'A'; std::string str(1, c);甚至是std::string str(1, 'A');。等等 -char转换为std::string。 -
向国际观众展示您的代码时请使用英文标识符。