In: Computer Science
As simple as possible. Please
In C++ Computers are playing an increasing role
in education.
Write a program that will help an elementary school student learn
multiplication. Use rand() to
produce two positive single digit integers. It should then display
a question in the following format
(assume 6 and 7 are randomly generated):
How much is 6 * 7?
The student then types the answer. Your program checks the
student’s answer. If it is correct, print
“Very good!” If the answer is wrong, print “No. Please try again.”
and then let the student
try the same question again repeatedly until the student finally
gets it right. If the number of
incorrect exceeds five, the program should leave the question,
display “Let’s try another
one!” and ask a different question. After every answer, the program
should ask whether the user
wants to continue the program and based on the response, the
program should be continued or
terminated.
C++ code:
#include<iostream>
using namespace std;
int main(){
while(true){
int number1 = rand()%10;
int number2 = rand()%10;
int ans = number1*number2;
int student_ans;
char quit = 0;
cout<<"How much is "<<number1<<" *
"<<number2<<"?\n";
cin>>student_ans;
if (student_ans == ans){
cout<<"Very good!\n";
cout<<"Do you want to continue? (Press q to quit or any other
key to continue)\n";
cin>>quit;
if (quit == 'q' || quit == 'Q')
return 0;
}
else{
for(int i=0;i<5;i++){
cout<<"No. Please try again\n";
cout<<"Do you want to continue? (Press q to quit or any other
key to continue)\n";
cin>>quit;
if (quit == 'q' || quit == 'Q')
return 0;
cout<<"Enter your answer: ";
cin>>student_ans;
if (student_ans == ans){
cout<<"Very good!\n";
cout<<"Do you want to continue? (Press q to quit or any
other key to continue)\n";
cin>>quit;
if (quit == 'q' || quit == 'Q')
return 0;
break;
}
}
if (student_ans != ans)
cout<<"Let’s try another one!";
}
}
return 0;
}
Output: