These are the codes that the prof gives us, I find out I could not get any value from the text,in the void InputGrades and CalculateStatistics. Therefore, I can not finish the following functions, like calculate the average.
Sorry for the long-long code I posed!~~:)
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
usingnamespace std;
//Declaring global variables in order to avoid having to pass arguments to functions
int numGrades; // Number of grades
float average; // Average grade
int highest; // Highest grade
int lowest; // Lowest grade
int aboveAverage; // Number of grades above the average
int belowAverage; // Number of grades below the average
int grades[101]; // Array of grades
// Declare function prototypes
// "Pre" means what must have been done before invoking the function
// "Post" means what will have been done upon returning from the function
void CalculateStatistics(ifstream& inData);
// Post: Data has been read and statistics calculated.
void OpenFiles(ifstream& inData, ofstream& outData);
// Post: File names have been prompted for and files are opened.
// Input file name has been written on output file
void InputGrades(ifstream& inData);
// Pre: inData is assigned and not empty
// Post: Grades have been read from inData,
// numGrades is the number of grades in inData
void CalculateAverage(float &average);
// Post: Average grade has been calculated
void CalculateHighest(int &highest);
// Post: Hightest grade has been calculated
void CalculateLowest(int &lowest);
// Post: Return value is the lowest grade
void CalculateAboveAverage(int &aboveAverage );
// Post: Number of grades above the average has been calculated
void CalculateBelowAverage(int belowAverage);
// Post: Number of grades below the average has been calculated
void PrintResults(ofstream& outData);
// Pre: outData has been opened
// Post: Output has been written on outData
int main()
{
// Declare and open files
ifstream inData;
ofstream outData;
OpenFiles(inData, outData);
if (!inData || !outData )
{
cout << "Files not opened successfully." << endl;
return 1;
}
CalculateStatistics(inData);
PrintResults(outData);
inData.close();
outData.close();
return 0;
}
//*********************************************************************
// Add the functions here.
void OpenFiles(ifstream & inData, ofstream& outData)
{
inData.open("grades.txt");
if(!inData)
{
cout<<"Open data fail."<<endl;
}
outData.open("output.txt");
if (!outData)
{
cout<<"Open output file fail."<<endl;
}
}
void CalculateStatistics (ifstream& inData)
{
int sum=0;
inData.open("grades.txt");
inData>>numGrades;
for (int i =0; i<numGrades;i++)
{
sum = sum+i;
}
cout<<sum;
}
void InputGrades(ifstream& inData)
{
for (int i=0;i<numGrades;i++)
{
inData>>grades[i];
}
}
void CalculateAverage()
{
int sum =0;
float avg;
for (int i=0; i<101;i++)
{
sum = sum+ grades[i];//?
}
cout<<"the average is: ";
avg = sum/101;
cout<<avg;
avg = average;
}
void CalculateHighest()
{
for (int i =0; i<numGrades;i++)
{
if(grades[i]>highest)
{
highest = grades[i];
}
}
cout<<"the highest value is :"<<highest;
}
void CalculateLowest()
{
for (int j =0; j<numGrades;j++)
{
if(grades[j]<lowest)
{
lowest = grades[j];
}
cout<<"the lowest grades is :"<<lowest;
}
}
void CalculateAboveAverage()
{
for(int k =0; k < numGrades; k++)
{
if (grades[k]>average)
{
grades[k] = aboveAverage;
cout<<aboveAverage<<" ";
}
}
}