Need help on text file reading

Hello everyone, I am working on a school assignment requiring us to process student records. However while choosing to list all the records from my menu, the list shown is incomplete, and I don't know what went wrong.

My code is as follows:

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstring>
#include<windows.h>
#include<cctype>

using namespace std;

typedef struct
{
char name[50];
char std_ID[7];
char course[50];
char phone[12];
char address[100];
char email[100];
char subject1[50];
char subject2[50];
char subject3[50];
char subject4[50];
char subject5[50];
char grade1[3];
char grade2[3];
char grade3[3];
char grade4[3];
char grade5[3];
}STUDENT_PROFILE;

void list(STUDENT_PROFILE stud[], int rows);
void update_grade(STUDENT_PROFILE stud[], int rows);
int searchStudent(STUDENT_PROFILE stud[], int rows);
void show_user();

int main(void)
{
ifstream inFile("studentrecords.txt");

if (!inFile)
cout << "Error opening input file\n";
else
{

STUDENT_PROFILE students[300];
int index = 0, choice;


while (inFile)
{

if (inFile.peek() == '\n')
inFile.ignore(256, '\n');
inFile.getline(students[index].name, 50);
inFile.getline(students[index].std_ID, 7);
inFile.getline(students[index].course, 50);
inFile.getline(students[index].phone, 12);
inFile.getline(students[index].address, 100);
inFile.getline(students[index].email, 100);
inFile.getline(students[index].subject1, 50);
inFile.getline(students[index].subject2, 50);
inFile.getline(students[index].subject3, 50);
inFile.getline(students[index].subject4, 50);
inFile.getline(students[index].subject5, 50);
inFile.getline(students[index].grade1, 3);
inFile.getline(students[index].grade2, 3);
inFile.getline(students[index].grade3, 3);
inFile.getline(students[index].grade4, 3);
inFile.getline(students[index].grade5, 3);
// read next number
index++;
}
inFile.close();
cout << "Number of student record = " << index << endl;//check number of student
// menu starts
do
{
cout << "1. List \n";
cout << "3. Update student grade\n";
cout << "4. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice)
{
case 1: list(students, index);
break;
case 2: show_user();
break;
case 3: update_grade(students, index);
break;
case 4: break;
default: cout << "Invalid choice\n";
}
} while (choice != 4);

ofstream outFile("studentrecords.txt");
if (!outFile)
cout << "Error opening output file, records are not updated.\n";
else
{
for (int i = 0; i < index; i++)
{
outFile << students[i].name << endl;
outFile << students[i].std_ID << endl;
outFile << students[i].course << endl;
outFile << students[i].phone << endl;
outFile << students[i].address << endl;
outFile << students[i].email << endl;
outFile << students[i].subject1 << endl;
outFile << students[i].subject2 << endl;
outFile << students[i].subject3 << endl;
outFile << students[i].subject4 << endl;
outFile << students[i].subject5 << endl;
outFile << students[i].grade1 << endl;
outFile << students[i].grade2 << endl;
outFile << students[i].grade3 << endl;
outFile << students[i].grade4 << endl;
outFile << students[i].grade5 << endl;
}
outFile.close();
}
}
return 0;
}

void list(STUDENT_PROFILE stud[], int rows)
{
system("cls");
for (int i = 0; i < rows; i++)
{
cout << "Student name : " << stud[i].name << "\n" << "Student ID : " << stud[i].std_ID << "\n";
cout << "Course : " << stud[i].course << "\n" << "Contect number : " << stud[i].phone << "\n";
cout << "Address : " << stud[i].address << "\n" << "E- mail : " << stud[i].email << "\n";
cout << "Subject" << setw(20) << "Grade" << endl;
cout << "" << stud[i].subject1 << "\t" << setw(20) << "" << stud[i].grade1 << "\n"
<< "" << stud[i].subject2 << "\t" << setw(20) << "" << stud[i].grade2 << "\n"
<< "" << stud[i].subject3 << "\t" << setw(20) << "" << stud[i].grade3 << "\n"
<< "" << stud[i].subject4 << "\t" << setw(20) << "" << stud[i].grade4 << "\n"
<< "" << stud[i].subject5 << "\t" << setw(20) << "" << stud[i].grade5 << "\n"
<< endl;
}
return;
}
void update_grade(STUDENT_PROFILE stud[], int rows)
{
cout << "test update ok";
}

int searchStudent(STUDENT_PROFILE stud[], int rows)
{
int cc;
cout << "test search ok";
cc = 0;
return 0;
}

void show_user()
{
cout << "show user";

}


and below is my output of my list

Student name : Michael J. Fox
Student ID : 12022
Course : Software Engineering
Contect number : 012-4787527
Address : Amherst Street, West Bloomfield, MI
E- mail : mikej@gmail.com
Subject Grade
Basic Japanese A
Basic C++ Programming B
Database Systems Fundamenta A
Game Engine Programming A+
Statistics for Computing A

Student name : Oscar Lee
Student ID : 12044
Course : Software Engineering
Contect number : 402-555-013
Address :
E- mail :
Subject
Last edited on
Editing your post to add code tags would probably go a long way towards getting useful answers. Plus without your input file we can't verify your problems.


Sorry about that, I am a new user. How do I format it so that it is in a more readable format?

Here is my input file:
Michael J. Fox
12022
Software Engineering
012-4787527
Amherst Street, West Bloomfield, MI
mikej@gmail.com
Basic Japanese
Basic C++ Programming
Database Systems Fundamenta
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Oscar Lee
12044
Software Engineering
402-555-0139
512 Winding Way,Cranford, NJ 07016
oscarl@hotmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
A
B
A+
A
Gloria Hamilton
12255
Software Engineering
402-555-0161
106 Aspen Court,Hastings, MN 55033
GHamilton@hotmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
B
B
A
A+
B
Yolanda Burton
12150
402-555-0156
Software Engineering
284 Mulberry Lane,Glen Ellyn, IL 60137
LBurton@yahoo.com
Basic German
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A+
B
A
A+
A
Lynne Burgess
12341
Software Engineering
402-555-0135
363 Church Street South,Mason City, IA 50401
lynne12@gmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B+
A-
A+
A
Tasha Casey
12431
Software Engineering
402-555-0154
845 Main Street East,Alexandria, VA 22304
tash_c@gmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A-
B
A-
A+
A-
Leticia Harvey
12098
Software Engineering
402-555-0188
614 Linden Avenue,Englewood, NJ 07631
l.harvey@gmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
B-
C+
C+
B
B
Irvin Collier
12567
Software Engineering
402-555-0181
578 William Street,Meriden, CT 06450
irvin.c@hotmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Abraham Cummings
12678
Software Engineering
402-555-0182
981 Queen Street,Venice, FL 34293
Abraham@gmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Terrence Taylor
12789
Software Engineering
402-555-0183
368 South Street,Villa Rica, GA 30180
terrencetaylor43@yahoo.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Johnny Nelson
12987
Software Engineering
402-555-0184
775 Fairview Road,La Vergne, TN 37086
johnnelson@hotmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Mark Sharp
12876
Software Engineering
402-555-0185
126 Route 32,Piscataway, NJ 08854
sharpiemark@gmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Shannon Ortega
12973
Software Engineering
402-555-0171
640 Hartford Road,Port Saint Lucie, FL 34952
shanortega@hotmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Israel Fisher
12853
Software Engineering
402-555-0173
511 Woodland Avenue,Desoto, TX 75115
zionisra@gmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Morris Stevens
12734
Software Engineering
803-555-0153
759 Locust Lane,Dawsonville, GA 30534
moorerist@hotmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Barry Wright
12889
Software Engineering
803-555-0159
168 Euclid Avenue,Palm City, FL 34990
flash341go@yahoo.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Leigh Mcdonald
12831
Software Engineering
803-555-0156
276 Grand Avenue,Saint Augustine, FL 32084
mcdonaldsleigh@gmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Bertha Cohen
12234
Software Engineering
803-555-0192
537 Mill Street,Anoka, MN 55303
bigbertha@yahoo.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Eugene Wise
12360
Software Engineering
803-555-0198
462 Brookside Drive,Elmont, NY 11003
wisesetup@yahoo.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Desiree Munoz
12100
Software Engineering
803-555-0195
517 Ann Street,Hackensack, NJ 07601
damexi@gmail.com
Basic French
Basic C++ Programming
Database Systems Fundamentals
Game Engine Programming
Statistics for Computing
A
B
A
A+
A
Last edited on
How do I format it so that it is in a more readable format?

See:

http://www.cplusplus.com/articles/z13hAqkS/
Is using the C-strings a requirement?

Because C-strings are a fixed length you need to insure your strings are large enough to hold the longest string. In your case it looks like one of your fields is not long enough to hold all the entries. The getline() function will put the stream into an error state if it doesn't encounter the delimiter, in this case the end of line character, and because one of your strings is not large enough getline() stops before it encounters this character.


Topic archived. No new replies allowed.