help in ifstream getline problem
Sep 11, 2013 at 8:20am UTC
I'm getting problems reading the lines in my file. The program reads the gradebook[x].IdNum perfectly. But when it gets to the getline statement, it doesn't read the data correctly. Here's my input:
1
2011-1-1312
Espinoza
Hayley Rose
T
9
7
98
86
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define N 51
struct gradebook_t {
string IdNum;
string lastname;
string firstname;
char MI;
int q1;
int q2;
int midex;
int finex;
} gradebook [N];
void printgrade (gradebook_t gbook);
int main (){
ifstream myfile;
int i, x;
myfile.open("LAB08.in" );
myfile >> i;
while (!myfile.eof()){
for (x=0;x<i;x++){
myfile >> gradebook[x].IdNum;
getline(myfile,gradebook[x].lastname);
getline(myfile,gradebook[x].firstname);
myfile >> gradebook[x].MI;
myfile >> gradebook[x].q1;
myfile >> gradebook[x].q2;
myfile >> gradebook[x].midex;
myfile >> gradebook[x].finex;
myfile.get();
}
break ;
}
for (x=0; x<i; x++){
if ((gradebook[x].q1 <= 10) && (gradebook[x].q2 <= 10) && (gradebook[x].midex <= 100) && (gradebook[x].finex) <= 100){
printgrade (gradebook[x]);
}
else
cout << "invalid grades for " << gradebook[x].IdNum << "\n\n" ;
}
myfile.close();
return 0;
}
void printgrade (gradebook_t gbook){
double qper, midper, finper, fgrade;
int ng;
qper = ((gbook.q1+gbook.q2) / 20.0) * 25.0;
midper = (gbook.midex / 100.0) * 25.0;
finper = (gbook.finex / 100.0) * 50.0;
fgrade = qper+midper+finper;
ng = static_cast <int >(fgrade + 0.5);
cout << "ID Number: " << gbook.IdNum;
cout << "\n" << "Name: " << gbook.lastname << ", " << gbook.firstname << " " << gbook.MI << "." ;
cout << "\n" << "Q1: " << gbook.q1;
cout << "\n" << "Q2: " << gbook.q2;
cout << "\n" << "ME: " << gbook.midex;
cout << "\n" << "FE: " << gbook.finex;
cout << "\n" << "NG: " << ng;
cout << "\n" << "LG: " ;
if (fgrade >= 90.0)
cout << "A" ;
if ((fgrade >= 80.0) && (fgrade < 90.0))
cout << "B" ;
if ((fgrade >= 70.0) && (fgrade < 80.0))
cout << "C" ;
if ((fgrade >= 60.0) && (fgrade < 70.0))
cout << "D" ;
if (fgrade < 60.0)
cout << "F" ;
cout << "\n\n" ;
}
it should output this one:
ID Number: 2011-1-1312
Name: Espinoza, Hayley Rose T.
Q1: 9
Q2: 7
ME: 98
FE: 86
NG: 88
LG: B
Instead it outputs this one:
ID Number: 2011-1-1312
H.spinoza
Q1:
Q2:
ME:
FE:
NG:
LG:
I don't know what's wrong. Please Help me.
Sep 11, 2013 at 8:34am UTC
the problem is line 32.MI is a char, but you file contains a string.
Topic archived. No new replies allowed.