I'm pretty new to programming and I'm having a little problem with an assignment. I've already wrote the program according to the instructors specs but I can't figure out one thing. He wants a sentinel value entered to end the program. I've done loops before but never with functions and I'm a little confused. Here is what the program looks like so far. If anyone can give me some suggestions on my looping problem I would greatly appreciate it.
#include <iostream>
#include <iomanip>
using namespace std;
//call functions
//getTestScore
getTestScore(testScore1, testScore2, testScore3);
//calAverage
average = calcAverage(testScore1, testScore2, testScore3);
//display average
displayAverage(average);
return 0;
} //end of main
//*****function definitions*****
void getTestScore(double &score1, double &score2, double &score3)
{
cout << "1st test score: ";
cin >> score1;
cout << "2nd test score: ";
cin >> score2;
cout << "3rd test score: ";
cin >> score3;
} //end of getTestScore function
double calcAverage(double test1, double test2, double test3)
{
double aver = 0.0;
aver = (test1 + test2 + test3) / 3;
return aver;
} //end of calcAverage function
void displayAverage(double aver)
{
cout << fixed << setprecision(1);
cout << "Average of test scores: " << aver << endl;
} //end of displayAverage function
Yeah, I hadn't put the loops in yet because i'm not sure where they go. I posted what I have done on the program so far. Everything is working fine except I need it to loop until a sentinel value is entered. I have an idea using an extra cout statement asking if the user would like to continue entering test scores with a (Y/N) answer, and I may use that but I was wondering about how to make it work with something like a while loop using a -1 sentinel value. This is what I came up with using the (Y/N) extra cout statement. Any ideas on using a while loop with a sentinel value like -1?
#include <iostream>
#include <iomanip>
using namespace std;
cout << "Would you like to enter test scores( Y/N): ";
cin >> makeEntry;
while (makeEntry == 'Y' || makeEntry == 'y')
{
//call functions
//getTestScore
getTestScore(testScore1, testScore2, testScore3);
//calAverage
average = calcAverage(testScore1, testScore2, testScore3);
//display average
displayAverage(average);
cout << "Would you like to enter test scores( Y/N): ";
cin >> makeEntry;
} //end while
return 0;
} //end of main
//*****function definitions*****
void getTestScore(double &score1, double &score2, double &score3)
{
cout << "1st test score: ";
cin >> score1;
cout << "2nd test score: ";
cin >> score2;
cout << "3rd test score: ";
cin >> score3;
} //end of getTestScore function
double calcAverage(double test1, double test2, double test3)
{
double aver = 0.0;
aver = (test1 + test2 + test3) / 3;
return aver;
} //end of calcAverage function
void displayAverage(double aver)
{
cout << fixed << setprecision(1);
cout << "Average of test scores: " << aver << endl;
} //end of displayAverage function
int input;
do{
//...
cout << "enter -1 to quit or another number to continue."
cin >> input;
}while( input != -1 );
It doesn't matter whether it's a while or a do-while loop. This will fail if the user enters something which is not a number (this flaw can be fixed with some extra code, of course).
Though the version with y/n is a lot more comfortable than -1..