In: Computer Science
Consider the following program.
// This program averages 3 test scores. It repeats as // many times as the user wishes. #include <iostream> using namespace std; int main() { int score1, score2, score3; // Three scores double average; // Average score char again; // To hold Y or N input do { // Get three scores. cout << "Enter 3 scores and I will average them: "; cin >> score1 >> score2 >> score3; // Calculate and display the average. average = (score1 + score2 + score3) / 3; cout << "The average is " << average << ".\n"; // Does the user want to average another set? cout << "Do you want to average another set? (Y/N) "; cin >> again; } while (again == 'Y' && again == 'y'); return 0; } |
The purpose of the program is to average three test scores and to keep prompting for them until the user does not enter Y or y at the bottom of the loop. Test the program thoroughly, and fix any unexpected behavior. Add a section of code that makes sure the user inputs a number between 0 and 100 for a test score and prompts the user for a new number if they enter a number outside the range of 0-100. For full credit you must do the following:
Hi.
The code is modified as asked.
Please add your name,date,email as comment at the top of the program as asked in the question.
2 versions of the code have been added(one using functions and one without using functions).
Either of them can be used.
Thank you.
Code Version-1(using functions):
/*There is an error in the given code for exiting loop condition.I have mentioned the reason in the comment at the loop.I have also modified the code so that only valid inputs are taken from the user(0-100) */
#include <iostream>
using namespace std;
//This function makes sure that the user enters scores between 0-100 only
double takeInput(){
double score; //to store score
cin>>score; //takes score from user
while(!(score>=0 && score <=100)){ //check if the entered number is not between 1 and 100
cout<<"Please enter score between 0 and 100"<<endl; //asks user to enter a valid number
cin>>score; //takes score from user
}
return score; //returns entered score
}
int main()
{
double score1, score2, score3; // Three scores(modified from int to double as scores can be in decimals)
double average; // Average score
char again; // To hold Y or N input
do
{
// Get three scores.
cout << "Enter 3 scores and I will average them: ";
score1=takeInput(); //calls takeInput function to take a valid score1
score2=takeInput(); //calls takeInput function to take a valid score2
score3=takeInput(); //calls takeInput function to take a valid score3
// Calculate and display the average.
average = (score1 + score2 + score3) / 3;
cout << "The average is " << average << ".\n";
// Does the user want to average another set?
cout << "Do you want to average another set? (Y/N) ";
cin >> again;
} while (again == 'Y' || again == 'y'); //modified '&&' to '||' as we need to check if user enters 'Y' OR 'y' , but previously it checked if user entered 'Y' AND 'y' which is not possible
return 0;
}
/*I have tested the code with the following values.
score1=100
The program asked me to enter a valid number between 0 and 100.So I entered
score1=20
score2=300
The program asked me to enter a valid number between 0 and 100.So I entered
score2=30
score3=-200
The program asked me to enter a valid number between 0 and 100.So I entered
score3=40
Then the program printed 'The average is 30'
*/
Code Version-2(without using functions):
/*There is an error in the given code for exiting loop condition.I have mentioned the reason in the comment at the loop.I have also modified the code so that only valid inputs are taken from the user(0-100) */
#include <iostream>
using namespace std;
int main()
{
double score1, score2, score3; // Three scores(modified from int to double as scores can be in decimals)
double average; // Average score
char again; // To hold Y or N input
do
{
// Get three scores.
cout << "Enter 3 scores and I will average them: ";
cin>>score1; //takes score from user
while(!(score1>=0 && score1 <=100)){ //check if the entered number is not between 1 and 100
cout<<"Please enter score between 0 and 100"<<endl; //asks user to enter a valid number
cin>>score1; //takes score from user
}
cin>>score2; //takes score from user
while(!(score2>=0 && score2 <=100)){ //check if the entered number is not between 1 and 100
cout<<"Please enter score between 0 and 100"<<endl; //asks user to enter a valid number
cin>>score2; //takes score from user
}
cin>>score3; //takes score from user
while(!(score3>=0 && score3 <=100)){ //check if the entered number is not between 1 and 100
cout<<"Please enter score between 0 and 100"<<endl; //asks user to enter a valid number
cin>>score3; //takes score from user
}
// Calculate and display the average.
average = (score1 + score2 + score3) / 3;
cout << "The average is " << average << ".\n";
// Does the user want to average another set?
cout << "Do you want to average another set? (Y/N) ";
cin >> again;
} while (again == 'Y' || again == 'y'); //modified '&&' to '||' as we need to check if user enters 'Y' OR 'y' , but previously it checked if user entered 'Y' AND 'y' which is not possible
return 0;
}
/*I have tested the code with the following values.
score1=100
The program asked me to enter a valid number between 0 and 100.So I entered
score1=20
score2=300
The program asked me to enter a valid number between 0 and 100.So I entered
score2=30
score3=-200
The program asked me to enter a valid number between 0 and 100.So I entered
score3=40
Then the program printed 'The average is 30'
*/
Sample Output: