New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have.
As part of a course I am doing at University I had to write a program in C++ that would allow the user to enter student information (matriculation number, name, status and mark), which was then stored in an array. They could then search it or list it. Everything was woking (and still is) up to here. We then had to add in two functions that would allow the user to save the array to disk and/or load it back from disk.
I added two functions (included below) int loadFile() and int saveFile() which would load a file from disk or save a file to disk. I can save the file to disk and when looking at the txt file everything appears to be OK. Here is a sample text file that it saves.
Expand|Select|Wrap|Line Numbers
- A001
- George Price
- P
- 99
- A002
- Fred Bloggs
- P
- 98
- A003
- Joe Public
- F
- 40
when it should be reading the full line. If I only enter the names as a single name everything works great. Therefore I think the error is in my loadaFile function.
I have tried both
Expand|Select|Wrap|Line Numbers
- infile >> studentsList[currentSize].name;
- // and
- getline(infile, studentsList[currentSize].name);
Here is the code that I think has the problem.
Expand|Select|Wrap|Line Numbers
- //declare structure type
- struct studentRecord{
- string matricNumber;
- string name;
- string status;
- int mark;
- };
- const int listSize = 30; //number of records in array
- int currentSize;
- //declare a list of records
- studentRecord studentsList[listSize]; //global array of records
- //function prototypes
- void addStudentRecords();
- void displayAllRecords();
- void displayMenu(int &option);
- void searchRecordMenu();
- void searchByMatric();
- void searchByName();
- void displaySearchMenu(int &choice);
- int saveFile();
- int loadFile();
Expand|Select|Wrap|Line Numbers
- int loadFile()
- {
- currentSize = 0;
- //open an input file
- ifstream infile("c:\\UniWorkspace\\students.txt", ios::in);
- // check if file is opened
- if(!infile) //return true if file is not opened
- {
- cout << "\nFailed to open file !!\n";
- cout << "Press any key to proceed\n";
- cin.get();
- return 1; //indicate program failed
- }
- while(!infile.eof()) //eof() End Of File function. Returns false if the end of file reached
- {
- infile >> studentsList[currentSize].matricNumber;
- infile >> studentsList[currentSize].name;
- // getline(infile, studentsList[currentSize].name); //tried alternate getline as name would normally have space in middle. This didn't work either!!
- infile >> studentsList[currentSize].status;
- infile >> studentsList[currentSize].mark;
- // The following lines were added to allow me to view the data coming in
- cout << "Loop count at " << currentSize << endl;
- cout << studentsList[currentSize].matricNumber << " "
- << studentsList[currentSize].name << " "
- << studentsList[currentSize].status << " "
- << studentsList[currentSize].mark << endl;
- // End of extra line - to be removed once working
- currentSize += 1; //increment record index
- cout << "\nPress any character to proceed\n";
- cin.get();
- }
- infile.close(); //close file
- currentSize = currentSize - 1;
- cout << "\nCurrentSize = " << currentSize << endl;
- cout << "Press any character to proceed\n";
- cin.get();
- return 0;
- }
- int saveFile()
- {
- int i;
- //create and open an output stream called students.txt
- ofstream outfile("c:\\UniWorkspace\\students.txt", ios::out);
- //check if file is opened
- if(!outfile) //return true if file is not opened
- {
- cout << "\nFailed to open file!!\n";
- cout << "\nPress any key to proceed ";
- cin.get();
- return 1;
- }
- for(i=0; i<currentSize; i++)
- {
- outfile << studentsList[i].matricNumber << endl;
- outfile << studentsList[i].name << endl;
- outfile << studentsList[i].status << endl;
- outfile << studentsList[i].mark << endl;
- }
- outfile.close(); //closes file
- cout << "\nFile closed" << endl;
- cin.get();
- return 0;
- }
Thanks in advance..
Geo