The program is a random number sum aggregator. It will present the user with two
ID: 441682 • Letter: T
Question
The program is a random number sum aggregator. It will present the user with two options, 1 or 0. Choosing the number 1 should generate a random number, display it, and add it to an overall "sum" variable. There should be a game loop that continues until the user enters 0, which will exit the loop, display the sum total of all the random numbers generated, and exit the program. Your program should have error checking in it such that if the user enters anything other than 1 or 0 it will display a message of "Invalid entry" and re-prompt the user.Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
srand(time((unsigned int)NULL));
int sum=0;
int choice;
int number;
do{
cout<<" Menu: 1. generate a random number to add to sum 0. exit"<<endl;
cout<<"Enter choice: ";
cin>>choice;
if(choice==1){
number=rand();
cout<<"Random number generated: "<<number<<endl;
sum+=number;
}
else if(choice>1||choice<0){
cout<<"Invalid entry"<<endl;
}
}while(choice!=0);
cout<<"overall sum: "<<sum<<endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.