Ok, went over my class notes and your reply is correct. I think I followed through correctly, but then came up with these errors when trying to compile.
Also, here's the way I re-coded it.
Code:
#include<iostream>
#include<fstream>
using namespace std;
//prototypes go here
void openFile (ifstream& inFile);
void testFile (ifstream& inFile);
void getScore(ifstream& inFile);
void calcAverage(int test1, int test2, int test3, int test4, int test5);
int findLowest(int test1, int test2, int test3, int test4, int test5);
int main()
{
//Open the file
//Test if file opened properly
//Read the test scores from the file
//Calculate the average
//Find the lowest test score
//Display the average of the four highest test scores
//Display the lowest of the five test scores
//Variables
ifstream inFile; //input file
int test1; //First test score input from the file
int test2; //Second test score input from the file
int test3; //Third test score input from the file
int test4; //Forth test score input from the file
int test5; //Fifth test score input from the file
int low; //Lowest of all test scores
int sum; //Calculated total of all test scores
float avg; //Calculated average of the four highest test score
//open the file
openFile(inFile);
//test if file opened properly
testFile(inFile);
//Read the test scores from the file
getScore(inFile);
int test = test1;
int test = test2;
int test = test3;
int test = test4;
int test = test5;
//Calculate the average
calcAverage(test1, test2, test3, test4, test5);
//Find the lowest test score
findLowest(test1, test2, test3, test4, test5);
//Display the computed and sorted scores
return 0;
}
// FUNCTION DEFINITIONS
/*************************
**openFile will open
**the file
**************************/
void openFile (ifstream& inFile)
{
inFile.open ("grades.txt");
}
/**********************************
**testFile will ascertain that
**the file opened properly
***********************************/
void testFile (ifstream& inFile)
{
if (!inFile)
{
cerr << "Error opening input file" << endl;
exit (2000); //Stops the program
}
}
/*************************************
**getScore will read one test score
**from the input file
**************************************/
void getScore(ifstream& inFile)
{
int test;
inFile >> test;
}
/***********************************
**calcAverage will calculate the
**average of the four test scores
***********************************/
void calcAverage(int test1, int test2, int test3, int test4, int test5)
{
int low = findLowest(test1, test2, test3, test4, test5);
int sum = test1 + test2 + test3 + test4 + test5 - low;
float avg = sum/4.0;
cout << "The average of the four highest scores is: " << avg << endl;
}
/***************************
**findLowest will sort out the
**lowest test score
****************************/
int findLowest(int test1, int test2, int test3, int test4, int test5)
{
int low = test1;
if (test2 < low)
{
low = test2;
}
else if (test3 < low)
{
low = test3;
}
else if (test4 < low)
{
low = test4;
}
else if (test5 < low)
{
low = test5;
}
cout << "The lowest score is: " << low << endl;
return low;
}
ERRORS
Code:
1>------ Build started: Project: P5hh, Configuration: Debug Win32 ------
1> P5hh.cpp
1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(48): error C2374: 'test' : redefinition; multiple initialization
1> i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47) : see declaration of 'test'
1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(49): error C2374: 'test' : redefinition; multiple initialization
1> i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47) : see declaration of 'test'
1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(50): error C2374: 'test' : redefinition; multiple initialization
1> i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47) : see declaration of 'test'
1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(51): error C2374: 'test' : redefinition; multiple initialization
1> i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(47) : see declaration of 'test'
1>i:\cop2000.c01 int to program\modules\mod6\p5hh\p5hh\p5hh.cpp(119): warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Originally Posted by
Elysia
At a glance, I can see these errors:
- GetScore returns void. That means it returns nothing. You can't return anything from the function and you cannot assign the return value from the function to a variable. Presumably, you meant for it to return int.
- Separate function declaration and function definition. Function declarations ends with a semicolon, but function definitions do not. GetScore and CalcAverage has this error.
- You declare variables before the actual function body of CalcAverage. All code in a function must come AFTER the opening brace.
- Consider passing the file as const reference to functions that do not modify the argument, like TestFile.