【发布时间】:2016-05-02 11:11:54
【问题描述】:
知道我的一个同学已经就这个主题发布了一个类似的问题,但我仍然无法理解这应该如何工作。 这是包含虚假学生信息的文件设置:
918273645,Steve,Albright,ITCS2530,MATH210,ENG140
123456789,Kim,Murphy,ITCS2530,MATH101
213456789,Dean,Bowers,ITCS2530,ENG140
219834765,Jerry,Clark,MGMT201,MATH210
由于某种原因,我只能读取文本文件的第一行,而不能读取下面的任何行。我需要弄清楚如何读取每行的前 9 个字符并将它们与用户输入进行比较。然后结转该行的其余部分。但无法弄清楚我哪里出错了。
这是我目前所拥有的:
void Login()
{
Student NewStudent;
ifstream inFile;
ifstream outFile;
string inFileName = "C:\\Users\\Prophet\\Desktop\\registration.txt";
string outFileName = "C:\\Users\\Prophet\\Desktop\\registration.txt";
openInputFile(inFile, inFileName);
while (true)
{
cout << "Please enter your student ID\n" << endl;
cin >> NewStudent.StudentID;
if (NewStudent.StudentID.length() == 9)
break;
else
cout << "That ID is invalid - IDs are 9 digits" << endl;
}
if (inFile.is_open())
{
while (!inFile.eof())
{
string line;
while (getline(inFile, line))
{
stringstream ss(line);
string StudentID, FirstName, LastName, ListOfCourses;
getline(ss, StudentID, ',');
getline(ss, FirstName, ',');
getline(ss, LastName, ',');
getline(ss, ListOfCourses, ',');
cout << "\n";
{
if (StudentID == NewStudent.StudentID)
{
cout << "Welcome to the Macomb Community College enrolment system " << FirstName << " " << LastName << endl;
inFile.close();
MainMenu();
}
if (StudentID != NewStudent.StudentID)
{
cout << "Welcome New student" << endl;
cout << "Please enter yuour first name: ";
cin >> NewStudent.FirstName;
cout << "Please enter yuour last name: ";
cin >> NewStudent.LastName;
outFile.open("C:\\Users\\Prophet\\Desktop\\registration.txt");
openOutputFile(outFile, outFileName);
MainMenu();
}
}
}
}
}
}
【问题讨论】:
-
请发帖minimal reproducible example。例如,
MainMenu()实际上是什么? -
您的代码仅读取一门课程(作为字符串),而在您的输入文件中有一个课程“列表”。因此,从该文件中读取的下一个学生 ID 实际上是一门课程。
-
MainMenu() 只是一系列 switch 语句。我无法弄清楚为什么我无法阅读第一行中的所有信息,例如课程列表。以及为什么我无法阅读下一行。我已经尝试了大约一周。