Global to local variables

Hey so how hard would it be to convert this code to have no global variables. I found out that my professor will not accept it if it has global. Also how would I convert it to having no local variables, thank you!

#include <iostream>
#include <cmath>
using namespace std;

//Function Prototypes.
void getJudgeData(double &); // Store in reference variables.
void calcScore(); // Calculate and return the average of the 3 scores.
double findHighest(); // Find and return the highest of the 5 scores
double findLowest(); // Find and return the lowest of the 5 scores.


// Variables
double score1, // Variable for the judges score.
score2,
score3,
score4,
score5,
average, // The average of the finalscore.
lowest,
totalscore, // All five scores added up.
finalscore, // The score total after the highest and lowest have been subtracted.
highest;

int main()
{
getJudgeData(score1);
getJudgeData(score2);
getJudgeData(score3);
getJudgeData(score4);
getJudgeData(score5);
calcScore();
cout << "The average score of the 5 judges is " << average << "\n\n";
system("pause");
return 0;
}
// Function used to get judge scores.
void getJudgeData(double &score)
{
do
{
cout << "Enter Judge's score: ";
cin >> score;
if ((score < 0) || (score > 10))
cout << "The score must be between 0 and 10 please try again.";
} while ((score < 0) || (score > 10));
}
// Function used to find the lowest number.
double findLowest()
{
if ((score1 < score2) && (score1 < score3) && (score1 < score4) && (score1 < score5))
lowest = score1;
else if ((score2 < score1) && (score2 < score3) && (score2 < score4) && (score2 < score5))
lowest = score2;
else if ((score3 < score1) && (score3 < score2) && (score3 < score4) && (score3 < score5))
lowest = score3;
else if ((score4 < score1) && (score4 < score2) && (score4 < score3) && (score4 < score5))
lowest = score4;
else if ((score5 < score1) && (score5 < score2) && (score5 < score3) && (score5 < score4))
lowest = score5;
return lowest;
}
//Function used to find the highest number.
double findHighest()
{
if ((score1 > score2) && (score1 > score3) && (score1 > score4) && (score1 > score5))
highest = score1;
else if ((score2 > score1) && (score2 > score3) && (score2 > score4) && (score2 > score5))
highest = score2;
else if ((score3 > score1) && (score3 > score2) && (score3 > score4) && (score3 > score5))
highest = score3;
else if ((score4 > score1) && (score4 > score2) && (score4 > score3) && (score4 > score5))
highest = score4;
else if ((score5 > score1) && (score5 > score2) && (score5 > score3) && (score5 > score4))
highest = score5;
return highest;
}
// Function used to get average.
void calcScore()
{
findLowest();
findHighest();
totalscore = (score1 + score2 + score3 + score4 + score5);
finalscore = (totalscore - lowest - highest);
average = finalscore / 3;
}
Move your variables into main and pass them around. If it was me I would just store my scores in an array and pass the array around.

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Not allowed to use array's haven't learn them yet
Topic archived. No new replies allowed.