Need some understanding.

This was an assignment I was given in my programming lab that is meant to use 3 functions to analyze the performance of a class from an input file. The user should be presented a menu that only ends with an input of '4' .

I got the program to compile but none of my functions work in the way intended.
I was wondering if anyone could give me some insight on what went wrong. The program has already been turned in im just trying to work on understanding now.

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
  // --------------------------------------------------------------------
// File name:   Performance.cpp
// Assign ID:   LAB10
// Due Date:    11/12/12 at 11:00 am 
//
// Purpose:     Create an analysis of the performance of a class
//
// Author:      bfields Byron Fields
// --------------------------------------------------------------------


#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

// -----------------------------------------------------
// Grades in input file
// 
// 3456 98 A
// 1234 33 F
// 9087 82 B
// 9212 80 B
// 1556 70 C
// 3444 99 A
// 9843 67 D
// -----------------------------------------------------

// ----------------------------------------------------
// Function meant to calculate average of grade in file.
//
// couldn't figure this one out at first so i just manually
// did the calculations for the function
// ----------------------------------------------------

float average(float& avg)
{
ifstream grade;
grade.open("grades.In");

avg = (98 + 33 + 82 + 80 + 70 + 99 + 67) / 7;

grade.close();
return avg;
}


// --------------------------------------------------------
// Funtion meant to find student number with highest grade
// --------------------------------------------------------
string highest(string& high)
{
ifstream grade;
grade.open("grades.In");
int student;
int gra;
char letter;
int higgh = 0;
int files = 7;
for(files; files > 0; files--)
{
grade >> student >> gra >> letter;
if( gra > higgh)
{
high = student;
higgh = gra;
}
 else
    cout << endl;
}

grade.close();
return high;
}

// ----------------------------------------------------------
// Funtion meant to count number of 'A' grades in class file
// ----------------------------------------------------------
int achievers(int& Acount)
{
ifstream grade;
grade.open("grades.In");
int student;
int gra;
char letter;
int files = 7;
for(files; files >= 0; files--)
{
grade >> student >> gra >> letter;
if( letter == 'A')
{
Acount++;
}
}
return Acount;
}

int main()
{

int menuchoice;
string high;
int Acount;
float avg;

cout << "What information would you like to get?" << endl;
cout << "1. The average grade of the class" << endl;
cout << "2. The number of the student with the highest grade" << endl;
cout << "3. The number of A's in the class" << endl;
cout << "4. Quit" << endl;

do
{
cin >> menuchoice;
if(menuchoice == 1)
{
average(avg);
cout << "You have chosen to find the average" << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
cout << "The average value is " << avg << endl;
}

else if(menuchoice == 2)
{
highest(high);
cout << "You want the student number with the highest grade" << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
cout << "The student number with the highest grade is " << high << endl;
}

else if(menuchoice == 3)
{
achievers(Acount);
cout << "You want to find the amount of A's in the class" << endl;
cout << "(c) 2012, bfields Byron Fields" << endl;
cout << "The number of A's is " << Acount << endl;
}
else if(menuchoice == 4)
{
cout << "You have reached the end of the program" << endl;
}

}while(menuchoice != 4);

return 0;
}
Last edited on
either read the file in each function:

Input File:
one student per line and the students values are seperated by spaces

Calculate average:
add the input after the first space of each read line and divide the sum by the amount of lines

student number with highest grade:
detect the maximum number after the first space of each line and save the lines index. then print the number before the 1st space of the line with the saved index.

Number of 'A' grades:
check the input after the 2nd space of each read line. if it's "A" increase your counting variable

or:
use 1 function for reading the file and getting the number, points or grade of each line saving them in a vector with 3 elements.
now you can implement the calculation functions simply passing the vector as a parameter each time
Last edited on
Topic archived. No new replies allowed.