Im trying to read in data using both getline and cin from a file. But, it only reads in the first line
and it leaves everything else empty and/or gives garbage values.
output:
John Brown grades: 73 57 94
grades: 0 32767 1252009800
grades: 0 0 1263344400
/*
data.txt
John Brown
73 57 94
Alex Smith
85 85 85
*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
class grades
{
public:
int quizzes[3];
int average;
};
class student
{
public:
grades q;
string name;
};
int findaverage(int *);
void readindata(student *, int &);
int main()
{
student students[30];
int size = 0;
readindata(students, size);
for(int i = 0; i < size + 1; i++)
{
cout << students[i].name << " grades: ";
int counter = 0;
while(counter < 3) //only the quizzes
{
cout << students[i].q.quizzes[counter] << ' ';
counter++;
}
cout << endl;
}
return 0;
}
void readindata(student *a, int &size)
{
ifstream file;
file.open("data.txt");
if(!file)
{
cout << "error..." << endl;
abort();
}
while(file.good())
{
getline(file, a[size].name);
int counter = 0;
while(counter < 3)
{
file >> a[size].q.quizzes[counter];
counter++;
}
size++;
}
file.close();
}