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.
// --------------------------------------------------------------------
// 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>
usingnamespace 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;
}
elseif(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;
}
elseif(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;
}
elseif(menuchoice == 4)
{
cout << "You have reached the end of the program" << endl;
}
}while(menuchoice != 4);
return 0;
}
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