Help Please! structures and ifstream.

I'm doing a program for school.

the program is a gradebook that uses structure which gets it's data from a file then displays everything needed. I tried doing the program without using first ifstream, and it worked perfectly. But when I try to use the stream input I get several errors. Please Help me! Thank you in advance!

here's a sample of the supposed to be input format from "LAB08.in"

The input begins with an integer N, 0 < N ≤ 50, which specifies the number of student records. Each record consists of eight lines:
ID number
last name
first name
middle initial
quiz 1 score
quiz 2 score
midterm exam score
final exam score

example:
3
2012-1-1000
Weston
Blake
N
6
6
60
60

2012-1-1098
Espinoza
Hayley Rose
T
10
10
100
100

2012-1-2312
Townsend
Nate
L
5
0
27
68

then here is the supposed to be display output

ID Number: 2012-1-1000
Name: Weston, Blake N.
Q1: 6
Q2: 6
ME: 60
FE: 60
NG: 60
LG: D

ID Number: 2012-1-1098
Name: Espinoza, Hayley Rose T.
Q1: 10
Q2: 10
ME: 100
FE: 100
NG: 100
LG: A

ID Number: 2012-1-2312
Name: Townsend, Nate L.
Q1: 5
Q2: 0
ME: 27
FE: 68
NG: 47
LG: F

and here's my code (I'm getting errors on the getline area)

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 {          //structure definition
  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");

while(!myfile.eof){            //getting the data from the file
  getline (myfile,i);
  for (x=0; x<i; x++)
  {
    getline(myfile,gradebook[x].IdNum);
	getline(myfile,gradebook[x].lastname);
	getline(myfile,gradebook[x].firstname);
	getline(myfile,gradebook[x].MI);
	getline(myfile,gradebook[x].q1);
	getline(myfile,gradebook[x].q2);
	getline(myfile,gradebook[x].midex);
	getline(myfile,gradebook[x].finex);
	cout << "\n";
  }
}
  
  for (x=0; x<i; x++){            //standard given grade policy
    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;     //stores the quiz, midterm and final exam percentage
int ng;

qper = ((gbook.q1+gbook.q2) / 20.0) * 25.0;     //computes for the average
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;         //displays info and computed grades
  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";
}
Topic archived. No new replies allowed.