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