In: Computer Science
Goals Practice loops and conditional statements Description Create a game for elementary school students to practice multiplication. The application should display a greeting followed by a set of mathematical questions. In each set the player is asked to enter a number from 1 to 9 to test multiplication skills or to press ‘E’ to exit the game. The application checks if the number entered is indeed between 1 and 9 and if not displays a message inviting the player to try again. Upon receiving a number in a proper range the application starts displaying multiplication questions with that number being multiplied by 0,1,2,…,10. For example, if the player entered number 3, the first question will be: “3 x 0 = ?”, the second: “3 x 1 = ?”, and so on with the last question being “3 x 10 = ?” At any point the player can press ‘E’ to exit current set For each question if an incorrect result is entered the application prompts the player to try again. The player can choose to try again or bypass. The application provides up to 4 replay attempts to answer the same question. For each question, the maximum score is 5 points if answered correctly. If not, each replay attempt lowers the score by 1 point. For example, if the player answered a question wrong and then got it right on the first replay attempt, the score is 4 points. Once the set is finished, a score for the set is presented and the player is invited to try another set.
Answer: Here is a C++ code for your
problem.
Hope this will help you :)
#include<bits/stdc++.h>
using namespace std;
int main(){
do{
cout<<"Welcome. Ready to Play?\nAt any moment you can press E to exit.\n";
char ch;
int ans=0; // final score
cout<<"Enter the number: ";
cin>>ch;
// exit the game when E is pressed
if(ch == 'E' || ch == 'e')
{
cout<<"Thank you for playing\n\n";
return 0;
}
int n = ch - '0';
// check if the number is invalid
if(n<1 || n>9)
{
cout<<"Invalid number. Number should be between(1-9)\n";
continue;
}
// multiply number entered to (0-10)
for(int i=0;i<=10;i++){
int score = 5;
// score for each replay will reduce by 1. Only 4 replays possible.
while(score>0){
cout<<n<<" x "<<i<<" = ?"<<endl;
string s;
cin>>s;
// exit the game when E is pressed
if(s == "E" || s == "e")
{
cout<<"Score: "<<ans<<"\nThank you for playing\n\n";
return 0;
}
int val = stoi(s);
if(i*n == val)
{
ans+=score; // score for 1 iteration
break;
}
score--;
if(score>0){
cout<<"TRY AGAIN! \n";
}
}
}
cout<<"Score: "<<ans<<endl<<endl;
}while(true);
return 0;
}