when i ask to enter choice ..the user inputs choice presses enter
and the program asks user to enter choice again.
i know its because of getChoice();
but how do i avoid it repeating the question and having the user input their answer again.?
(its in the last function by the way displayResult(); )
#include <iostream>
using namespace std;
void getScores( double testScores[], int size);
void showMenu();
char getChoice();
void displayResult(double testScores[], int size);
int main()
{
const int SIZE = 5;
double testScores[SIZE];
char choice;
getScores(testScores, SIZE);
showMenu();
getChoice();
displayResult( testScores,SIZE);
system("pause");
return 0;
}
void getScores( double testScores[], int size)
{
size = 5;
cout << "Enter " << size << " test scores.\n";
for(int i = 0; i < size; i++)
cin >> testScores[i];
system("CLS");
}
void showMenu()
{
cout << "\nA.) Calculate the average of the test scores."
<< endl << "B.) Display all test scores\n\n" ;
}
char getChoice( )
{
using namespace std;
char choice;
cout << "Enter your choice: \n";
cin >> choice;
return(choice);
system("CLS");
}
void displayResult( double testScores[], int size)
{
system("CLS");
using namespace std;
char choice = getChoice();
system("CLS");
double total = 0;
if(toupper(choice) == 'A')
{
for(int i = 0; i < size; i++)
total = total + testScores[i];
cout << "The average: " << total/size << "\n";
}
else if(toupper(choice) == 'B')
{
for(int i = 0; i < size; i++)
{
cout << "The test scores are: " << " ";
cout << testScores[i] << endl;
}
}
else if(toupper(choice) != 'B' ||toupper(choice) != 'A')
{
cout << "Invalid entry!" << endl;
}
}
You're calling it twice; once in main()
and once in displayResult()
.