Really need advice as to why this isnt working

This is the file setup that contains fake student information:

918273645,Steve,Albright,ITCS2530,MATH210,ENG140
123456789,Kim,Murphy,ITCS2530,MATH101
213456789,Dean,Bowers,ITCS2530,ENG140
219834765,Jerry,Clark,MGMT201,MATH210

For some reason i am only able to read the first line of the text file and not any of the lines below. I need to figure out how to read the first 9 characters of each line and compare them to the users input. Then carry over the rest of that line. but cant figure out where I'm going wrong.

For example, the user enters "123456789" for their ID, and Kim Murphy's information is then read. Upon the first iteration of getline, "ITCS2530" is read and put into the variable, and is then loaded into the struct; no problem there. However, the last course in the list has the newline character before the next comma, so the next iteration reads "MATH101/nl213456789" and puts the entire string into the variable and tries to load that into the struct.

I think i am severely over complicating this ,but i just frustrated and confused at this point.

Here is the code that I am currently working on:


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();


}
}
}
}
}
}
Last edited on
Topic archived. No new replies allowed.