Hi, I am having a very hard time understanding what I am doing wrong here.
I am trying to read from a file into a struct array, however it stops working properly after a short while.
my data in the file is in the following format:
Last, First Name
ID Number
4 grades recieved
and so on...
specific example
Doe, John
123-4567
7 10 6 7
Doe, Jane
098-7654
7 7 7 6
here is my code so far...
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip> // do i need last 3?
#include <stdlib.h>
#include <ctype.h>
using namespace std;
const int MAX_NAME_LENGTH = 16; //actual is 16
const int ID_NUMBER_LENGTH = 9; //actual is 9
struct st_Student_Info //structure decloration
{
char name[MAX_NAME_LENGTH]; //name of student last, first
char idNumber[ID_NUMBER_LENGTH]; //student ID number
int grade1; //grades for student
int grade2;
int grade3;
int grade4;
};
typedef struct st_Student_Info StudentInfo;
const int MAX_DB_SIZE = 31; //actual size is 31
StudentInfo currDB[MAX_DB_SIZE];
//begin function prototypes
int ReadDB(const int DBSize);
int main()
{
int nStudents = ReadDB(MAX_DB_SIZE);
cout << nStudents << endl;
return 0;
}
int ReadDB(const int DBSize)
{
ifstream fIn;
fIn.open("student.dat"); //input
if (!fIn)
{
cout << "Can't open txt file." << endl;
return -99;
}
int i = 0; //counter
for(i = 0; i< DBSize; i++)
{
fIn.getline (currDB[i].name, MAX_NAME_LENGTH);
fIn.getline (currDB[i].idNumber, ID_NUMBER_LENGTH);
fIn >> currDB[i].grade1 >> currDB[i].grade2 >> currDB[i].grade3 >> currDB[i].grade4;
cout << "n " << currDB[i].name << endl;
cout << "# " << currDB[i].idNumber << endl;
cout << "1 " << currDB[i].grade1 << endl;
cout << "2 " << currDB[i].grade2 << endl;
cout << "3 " << currDB[i].grade3 << endl;
cout << "4 " << currDB[i].grade4 << endl;
}
fIn.close();
return i;
}
im pretty sure my errors are caused by the .getline and >> but i cant tell.
any help is appreciated!