getline command and grade function

First, I cannot figure out how to use getline to read data such as the following past the first name and last name. I can get the first name and last name to print of the first student, but after that it only prints the first name and zeros of the id and scores of the second student and does not even print the rest of the students

Jon Doe
2838596
93 85
Jane Smith
9493853
87 91
Kaitlyn Davies
4968493
80 78
Amanda Warden
5347646
67 98

Also I do not know how to print the letter grade of each score beside the number grade

We must have functions that read the data, determine the grade, compute the average of the class, and print the results

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
#include "prog5.h"

// function prototypes
void init_students(StudentGrade []); //init all stud data
void read_data(StudentGrade [], int&); // read data from file
void print_result(const StudentGrade [], int); //print report
float avg(const StudentGrade [], int); // finding the class average
char grade(const StudentGrade [], int);
int main()
{
StudentGrade studList[MAX_STDS];
int std_num; // number of student data read from file

init_students(studList);
read_data(studList, std_num);
avg(studList, std_num);
grade(studList, std_num);
print_result(studList, std_num);

return 0;
}

void init_students(StudentGrade st[])
{
// initialize each data member to zero or equivalent
int i;
for (i = 0; i < MAX_STDS; i++) {
st[i].name = "";
st[i].number = 0;
st[i].eng101 = 0;
st[i].hist201 = 0;
}
}
void read_data(StudentGrade st[], int& num)
{
ifstream inFile;
string fileName;

// ask the user for the name of the file
cout << "Enter the name of the file that contains the data: ";
cin >> fileName;

// open the file
inFile.open(fileName.data());
// error check
while (!inFile) // see if the file can be opened
{
cout << "File does not exist!" << endl;
cout << "Enter the name of the file: ";
cin >> fileName;
inFile.open(fileName.data());
} // end while

// reading begins
int i = 0;
getline(inFile, st[i].name);
while (inFile) {
inFile >> st[i].number >> st[i].eng101 >> st[i].hist201;
i++;
inFile >> st[i].name;
} // end while
num = i;
}

float avg(const StudentGrade st[], int num)
{
int sumEng = 0;
int sumHist = 0;
float avgEng = 0.0;
float avgHist = 0.0;
int i;

for (i = 0; i < num; i++)
{
sumEng = sumEng + st[i].eng101;
sumHist = sumHist + st[i].hist201;
}
cout << "Sum Eng" << sumEng <<endl;
cout << "Sum His" << sumHist <<endl;

num = i;
avgHist = sumHist/num;
avgEng = sumEng/num;
cout << "Avg hist is: " << avgHist << endl;
cout << "Avg eng is: " << avgEng <<endl;
}

char grade(const StudentGrade st[], int num)
{
int i;
char engScore;
char histScore;
for (i = 0; i < num; i++)
{
if(st[i].eng101 >= 90)
{
engScore = 'A';
}
else if(st[i].eng101 >= 80)
{
engScore = 'B';
}
else if(st[i].eng101 >= 70)
{
engScore = 'C';
}
else if(st[i].eng101 >= 60)
{
engScore = 'D';
}
else
{
engScore = 'F';
}
cout << engScore <<endl;
}
for (i = 0; i < num; i++)
{
if(st[i].hist201 > 90)
{
histScore = 'A';
}
else if(st[i].hist201 > 80)
{
histScore = 'B';
}
else if(st[i].hist201 > 70)
{
histScore = 'C';
}
else if(st[i].hist201 > 60)
{
histScore = 'D';
}
else
{
histScore = 'F';
}
cout << histScore <<endl;
}
}
void print_result(const StudentGrade st[], int num)
{
int sumEng = 0;
int sumHist = 0;
float avgEng = 0.0;
float avgHist = 0.0;
int i;
//print header
cout << "Student Name " << "Student ID " << "Eng101 "
<< "Hist201" <<endl;
cout << "------------" << " ----------" << " ------"
<< " -------" <<endl;

for (i = 0; i < num; i++) {
cout << st[i].name << " " << st[i].number
<< " " << st[i].eng101 << " "
<< st[i].hist201 <<endl;

sumEng = sumEng + st[i].eng101; // sum for English scores
sumHist = sumHist + st[i].hist201; // sum for History scores

}

}
Last edited on
We had a question like this before...

st is a struct, correct? Could we get it's contents?
As for printing the letter grade, what problems are you having with that, exactly? EDIT: Aren't you storing the letter in a char from which you could access and print it just like that?

-Albatross

P.S.- Am I the only one who finds st[i] a little bit funny?
Last edited on
I'm sorry about that
1
2
3
4
5
6
7
8
const int MAX_STDS = 20;

struct StudentGrade {
   int number;          // student number
   string name;         // student name
   float eng101;
   float hist201;
};


I know it's a stupid question, but I can't quite think right now so how would I be able to access and print each individual letter grade

the output should look similar to this

1
2
3
4
5
Student Name	Student ID		Eng101	Hist201
     Jon Doe	 xxxxxxxxx		 93(A)	  85(B)
  Jane Smith	 yyyyyyyyy		 87(B)	  91(A)
         ...
			Class Average:     ...		  ...
Last edited on
Can someone please help me? This is due very soon and I am stuck and frustrated
For your struct, insert a variable named char letter; (for example, but it has to be a char). Assuming you know how to compute the letter grade, after computing it you can put it into your char that you have in your struct, and then just read it out along with all the other data.
If you are forbidden from modifying your struct, then create a char[] outside of any functions (some will say this is evil, but...)

Speaking of which, you should declare your avg variables outside of all the functions as well.

@Anyone who thinks global variables are evil: Ridiculously long lists of arguments are evil too, no? Which makes the code harder to follow: a few variables declared on the outside of all the functions, or a function or two that requires so many arguments that it's hard to keep track of what goes where?

I hope that helps.

-Albatross

Topic archived. No new replies allowed.