Sorry if this seems dumb, I am pretty new to C++ and I'm having trouble displaying information read into an array of structures from an input file.
I have 3 functions. One for reading my file into the array, one for prompting the user to search for a specific struct in the array, and the last one for displaying the contents of the array.
I don't know for certain if my readFile()
or displayAllStudents()
is broken. I am able to output the first line from the file, but the rest is 0. Next my selectStudent()
is probably terrible; I'm having trouble finding a good solution for what I'm trying to accomplish.
I've tried many solutions posted here, but my issue seems unique so I'm hoping I can help pointed in the right direction.
Input file and desired output.
ID CLA OLA Quiz Homework Exam Bonus Total FinalGrade
c088801 10 15 4 15 56 5
c088802 9 12 2 11 46 2
c088803 8 10 3 12 50 1
c088804 5 5 3 10 53 3
c088805 3 11 1 10 45 0
c088806 8 14 2 11 40 -1
c088807 4 12 2 12 48 -2
c088808 10 10 3 11 36 0
c088809 8 8 3 11 39 0
c088810 6 9 4 9 47 3
c088811 8 7 3 13 41 3
c088812 4 11 3 11 37 1
c088813 9 15 2 8 50 2
c088814 8 12 2 10 48 4
c088815 6 8 1 7 45 1
c088816 7 7 2 6 51 2
c088817 8 9 2 12 38 2
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student {
char ID[7];
int CLA;
int OLA;
int Quiz;
int Homework;
int Exam;
int Bonus;
int Total;
int FinalGrade;
};
const int SIZE = 20;
//Function prototypes
void readFile(Student[]);
int selectStudent(Student[]);
void displayAllStudents(Student[]);
int main() {
Student Roster[SIZE] = {}; //Initalizes array
readFile(Roster);
//selectStudent(Roster);
displayAllStudents(Roster);
system("pause");
return 0;
}
//This function will read the text file into our array of structures.
void readFile(Student Roster[]) {
ifstream inFile("point.dat"); //Reads input file
string line;
getline(inFile, line); //Skips first line of file
for (int i = 0; i < SIZE; i++) {
inFile >> Roster[i].ID >> Roster[i].CLA >> Roster[i].OLA >>
Roster[i].Quiz >> Roster[i].Homework >> Roster[i].Exam >>
Roster[i].Bonus >> Roster[i].Total >> Roster[i].FinalGrade;
}
}
//This function will prompt user to select an individual student ID
//then display that student's information.
int selectStudent(Student Roster[]) {
char* cNumber;
cout << "Please enter the student's c-number:" << endl;
cin >> cNumber;
for(int i; i < SIZE; i++){
if(strcmp(Roster[i].ID, cNumber)==0){
return i;
}
}
return -1;
}
//This function will display all our student information
void displayAllStudents(Student Roster[]) {
cout << " ID CLA OLA Quiz Homework Exam Bonus Total FinalGrade" << endl;
for (int i = 0; i < SIZE; i++) {
cout << Roster[i].ID[0] << Roster[i].ID[1] << Roster[i].ID[2] << Roster[i].ID[3] << Roster[i].ID[4] << Roster[i].ID[5] << Roster[i].ID[6]
<< " " << Roster[i].CLA << " " << Roster[i].OLA << " " << Roster[i].Quiz << " " <<
Roster[i].Homework << " " << Roster[i].Exam << " " << Roster[i].Bonus << " " <<
Roster[i].Total << " " << Roster[i].FinalGrade << endl;
}
}
My output.
ID CLA OLA Quiz Homework Exam Bonus Total FinalGrade
c088801 10 15 4 15 56 5 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Press any key to continue . . .