Sep 20, 2011 at 12:19am UTC
I am beginning c++ trying to debug program to average the HIGHEST 4 out of 5 test scores. I CAN'T use arrays. Here is my code. It will NOT compile. PLEASE HELP :-))
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void getScore(int &score);
int findLowest(int, int, int, int, int);
void calcAverage(int, int, int, int, int, int);
int main ()
{
int score1, score2, score3, score4, score5, lowest;
getScore(score1);
getScore(score2);
getScore(score3);
getScore(score4);
getScore(score5);
findLowest(score1, score2, score3, score4, score5);
calcAverage(score1, score2, score3, score4, score5, lowest);
system("pause");
return 0;
}
void getScore(int &score)
{
cout << "Please enter test score (Please enter value between 1 and 100): ";
cin >> score;
while (score < 1 || score > 100)
{
cout << "ERROR: Please enter Test score values 0 to 100! ";
cin >> score;
}
}
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
int lowest = score1;
{
if (score2 < lowest)
lowest = score2;
else if (score3 < lowest)
lowest = score3;
else if (score4 < lowest)
lowest = score4;
else if (score5 < lowest)
lowest = score5;
}
cout << "The lowest test score is " << lowest << endl;
return lowest;
}
void calcAverage (int score1, int score2, int score3, int score4, int score5, int lowest)
{ double average;
average = (((float)score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;
//Numeric output
cout << setw(4);
cout << fixed << showpoint << setprecision(2);
cout << "The average of the 4 highest test scores entered is: " << average << endl;
}
Sep 20, 2011 at 12:32am UTC
Codes tags and error messages please.
Sep 20, 2011 at 8:02pm UTC
Warning 1 warning C4700: uninitialized local variable 'lowest' used
2 IntelliSense: argument of type "int *" is incompatible with parameter of type "int"
Thanks SO MUCH for any help! I think I need call the findLowest function from inside the calcAverage function so here is my new code. Error codes are above. I can't use arrays so it's puzzling esp. since I am a newbie ;-)
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void getScore(int &score);
void calcAverage(int, int, int, int, int);
int main ()
{
int score1, score2, score3, score4, score5;
getScore(score1);
getScore(score2);
getScore(score3);
getScore(score4);
getScore(score5);
calcAverage(score1, score2, score3, score4, score5);
system("pause");
return 0;
}
void getScore(int &score)
{
cout << "Please enter test score (Please enter value between 1 and 100): ";
cin >> score;
while (score < 1 || score > 100)
{
cout << "ERROR: Please enter Test score values 0 to 100! ";
cin >> score;
}
}
int findLowest(int score1, int score2, int score3, int score4, int score5, int lowest)
{
lowest = score1;
if (score2 < lowest)
lowest = score2;
else if (score3 < lowest)
lowest = score3;
else if (score4 < lowest)
lowest = score4;
else if (score5 < lowest)
lowest = score5;
cout << "The lowest test score is " << lowest << endl;
return lowest;
}
void calcAverage (int score1, int score2, int score3, int score4, int score5)
{
int findLowest(int, int, int, int, int, int);
int lowest;
double average;
findLowest(score1, score2, score3, score4, score5, &lowest);
average = (((float)score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;
//Numeric output
cout << setw(4);
cout << fixed << showpoint << setprecision(2);
cout << "The average of the 4 highest test scores entered is: " << average << endl;
}
Sep 20, 2011 at 8:06pm UTC
Now, the code tags. Look at the "format" thingies at the right side if you don't know how to use them.
Sep 20, 2011 at 10:02pm UTC
I copy pasted the program to Dev C++ IDE, and it compiled.
So, I don't know why it is not compiling on your computer.
Although, I must say, this program could have been written in a much more concise manner, without so many functions.