For our last assignment of the year our prof asked us to complete a program which calculates various statistics drawn from a text file. The text file contains 75 grades, and was given to us to compare with sample output also given. The functions, such as finding maximum, number of marks above average, etc., are of little concern to me, as I've done similar things in smaller programs before. However, it is the functions 'OpenFiles' and 'InputGrades' which are of concern.
Assuming my OpenFiles works (which it does for me, knowing the pathname for the txt file on my laptop), calling the two functions in question will result in the files being opened, and then the grades in the file (all 75) will printed. I print them just to confirm that the grades are in fact retrieved from the txt file.
Finally, my question is how to store/convert these grades, currently in the character array 'data', into the global variable int grades[101] that was included in the code our professor gave us. The goal afterwards is to use the 'grades' array as an argument for the various 'Calculate' functions.
Sorry for being long-winded, but any help would be appreciated.
Thanks in advance.
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
|
This code was supplied by the professor.
//******************************************************************
// This program calculates the average, high score, low score,
// number above the average, and number below the average for
// a file of test scores.
// Assumption: File contains at least one non-zero value
//******************************************************************
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace 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();
// Post: Average grade has been calculated
void CalculateHighest();
// Post: Hightest grade has been calculated
void CalculateLowest();
// Post: Return value is the lowest grade
void CalculateAboveAverage();
// Post: Number of grades above the average has been calculated
void CalculateBelowAverage();
// 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.
The two functions I am having trouble with:
void OpenFiles(ifstream& indat, ofstream& outdat)
{
string fname;
cout << "Enter file name: " ;
getline(cin, fname);
indat.open(fname);
if(!indat)
{
cout << "File was not opened." << endl;
}
outdat.open("C:/Users/Sean/Desktop/pocketcpp/A5durr.txt");
if (!outdat)
{
cout << "YARRR! Didn't work!" << endl;
}
}
void InputGrades(ifstream& indat)
{
numGrades = 75;
char data[100] = {0};
for (int i = 0; i < numGrades; i++)
{
indat >> data;
cout << data << ' ';
}
}
|