0

know that a classmate of mine has already posted a similar question on this topic ,but i still cant wrap my mind around how this is supposed to work. 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.

This is what i have so far:

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


                    }
                }
            }
        }
    }
}
Juan
  • 1
  • 2
  • 1
    Please post a [MCVE]. What does `MainMenu()` actually for instance? – πάντα ῥεῖ May 02 '16 at 11:16
  • your code read only one course (as string) while in your input file there is a "list" of courses. So the next student Id read from that file is actually a course. – Bob__ May 02 '16 at 11:20
  • MainMenu() is just a series of switch statements. What i cant figure out is why i cant read all the information in the first line ,such as the list of courses. and also why i cant read the next line. ive been trying for about a week. – Juan May 02 '16 at 11:22

2 Answers2

0

In your main loop which reads the file, you break when StudentID == NewStudent.StudentID and you break when StudentID != NewStudent.StudentID, which means that you always finish the loop after reading the first line.

When reading in fields from line based records, it is always easier to first read in the line and store it in a stringstream and then read the fields from there. But that won't help unless you change your code to only stop when the id is found.

Remark that while (!inFile.eof()) can give wrong results. You are better of checking the read results as already stated many times on this site.

Community
  • 1
  • 1
stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • Thank you i have removed the break statements and add a string-stream but i still cant figure out how this should look. Still only reading the first line – Juan May 02 '16 at 12:16
  • 1
    You should first read all lines to check whether the ID can be found. Only after reading all lines, you can decide that the ID was not found and if that is the case, you must append a line and not overwrite the complete file. – stefaanv May 02 '16 at 14:39
0

You are currently using break in your loop, so after first line it's not going to second line. Instead of break you have to use continue.

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Adesh
  • 34
  • 4