Question

In: Computer Science

die's job is to output an error message alarmingly and terminate the program. You may copy...

die's job is to output an error message alarmingly and terminate the program. You may copy my

function definition, or use your own:

// The following 4 lines should be present if we have a die function:

#include <iostream> // cout, endl

#include <string> // string

#include <cstdlib> // exit, EXIT_FAILURE

using namespace std;

bool die(const string & msg){

cout <<"Fatal error: " <<msg <<endl;

exit(EXIT_FAILURE);

}

4. Use rand to randomly choose one of the five operators * / % + - .

Use rand again to choose a random int in the range [1, 20].

Use rand again to choose a second random int in the range [1, 20].

Ask the user what the answer to the corresponding equation is.

C&d on input failure

Tell the user whether he's correct.

An example run of your program might go as

What's the answer to 14 * 3 ?

42

Yes, that's the right answer!

Solutions

Expert Solution

4>

C++ Code:

Output Snapshot:

Text Code:

#include <iostream>
using namespace std;

int main()
{
// Character Variable to store operatorSymbol
char operatorSymbol[6] = {'*','/','%','+','-'};
// Generating random number between 0 to 4 and storing it in operatorRandomNum
int operatorRandomNum = rand() % 5;
// Generating random number between 1 to 20 and storing it in firstRandomNum
int firstRandomNum = rand() % 20 + 1;
// Generating random number between 1 to 20 and storing it in secondRandomNum
int secondRandomNum = rand() % 20 + 1;
// Variable to store user input answer
int userInputResult;
// Prompting user to provide answer
cout << "What's the answer to " << firstRandomNum
<< " " << operatorSymbol[operatorRandomNum] << " " << secondRandomNum << "?" << endl;
// Storing user input in userInputResult variable
cin >> userInputResult;
  
// Variable to store actual calculated correct answer
int actualResult;
switch(operatorRandomNum){
case 0: // * Multiplication Operation
actualResult = firstRandomNum*secondRandomNum;
break;
case 1: // / Division Operation
actualResult = firstRandomNum/secondRandomNum;
break;
case 2: // % Modulo Operation
actualResult = firstRandomNum%secondRandomNum;
break;
case 3: // + Addition Operation
actualResult = firstRandomNum+secondRandomNum;
break;
case 4: // * Subtraction Operation
actualResult = firstRandomNum-secondRandomNum;
break;
}
  
// Displaying if user provided answer is correct or not
if(userInputResult == actualResult){
cout << "Yes, that's the right answer!";
} else {
cout << "No, that's the wrong answer!";
}
return 0;
}

Explanation:

  • The program uses rand() function to generate random values. But the problem with rand() function is that, it generates the same value on every compilation and run.
  • rand() % 20 generates the random value between range 0 to 19, and adding 1 to the expression (rand() % 20+1) results in generating a random value between 1 to 20.
  • To generate different value values for every compilation and run process, please use srand() function instead of rand().
  • Please replace the rand() by srand(time(NULL)) to generate a new random number for every compilation and run process. As srand() function uses Seed to generate the random number, In the seed, the time value is passed for seed, Hence it generates different random value with time whenever code is compiled and run.
  • Please refer code comments for understanding purpose.

Related Solutions

trace through the program and what the output would be. If there is an error explain...
trace through the program and what the output would be. If there is an error explain what to change. #include <iostream> using namespace std; int fun(int c, int b); int main(){ int a = 0, b= 5, c = 10; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; b=fun(a, c); cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; while(b==21){ int a = 3; b = a; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; } cout<<"a is:...
Whenever I am attempting to write a simple program on C++ I get an error message...
Whenever I am attempting to write a simple program on C++ I get an error message that reads "cout was not declared in this scope". Literally every time. This has become frustrating because I have even written my code the exact same way as some of my classmates who got theirs to compile and run with no sign of this error at all, and yet min gives this answer. I will leave an example of a code where this error...
These problems may be solved using Minitab. Copy and paste the appropriate Minitab output into a...
These problems may be solved using Minitab. Copy and paste the appropriate Minitab output into a word-processed file. Add your explanations of the output near the Minitab output. DO NOT SIMPLY ATTACH PAGES OF OUTPUT AS AN APPENDIX. Each problem should be able to fit on one or two pages, and each problem should include the following: Minitab output for the ANOVA. Written statement interpreting the ANOVA. Four-in-one plot of the residuals. Written interpretation as to whether the three assumptions...
Creation of Program Application (Development Task 1) This program should create two output files. You may...
Creation of Program Application (Development Task 1) This program should create two output files. You may use .txt files. Create the following files: Deposists.txt Withdrawls.txt 1. The program application should have the following inputs: Amount for Deposits (Only Accept Positive Amounts) Amount for Withdrawals (Only Accept Positive Amounts). The subsequent program will subtract the positive amount. 2. The program application should have the following outputs: Deposists.txt Withdrawals.txt Total of Deposits and Withdrawals back to the console
In python can you fix the error in the line where it says message = input("Input...
In python can you fix the error in the line where it says message = input("Input a lowercase sentence: ") this is the error message I get after running the program and inputing a lowercase sentence Input a lowercase sentence:hello hi MM§MTraceback (most recent call last): MM§M File "client.py", line 14, in <module> MM§M message = input("Input a lowercase sentence:") MM§M File "<string>", line 1 MM§M hello hi MM§M ^ MM§MSyntaxError: unexpected EOF while parsing from socket import * #...
What caused the following error message to appear in Microsoft access do not merely state the name of the error indicate why the error occurred and what the user was doing when this message appeared?
What caused the following error message to appear in Microsoft access do not merely state the name of the error indicate why the error occurred and what the user was doing when this message appeared?
What are the six major categories of factors that may influence the decision to terminate a...
What are the six major categories of factors that may influence the decision to terminate a project early? Provide examples of each.
What does it mean when you get an error message running SPSS forbinary linear regression...
What does it mean when you get an error message running SPSS for binary linear regression that states the dependent variable has more than two non-missing values. For logistic regression, the dependent variable value must assume exactly two values on the cases being processed. What do I need to do to fix this issue?
At the command prompt, type cd etc and press Enter. What error message did you receive...
At the command prompt, type cd etc and press Enter. What error message did you receive and why?
Every answer shown for this question appears to be giving an error message or locking the...
Every answer shown for this question appears to be giving an error message or locking the database or telling me there is an operational error or undefined parameter of some sort. Here is the question. This is for Python3 Define the Artist class in Artist.py with a constructor to initialize an artist's information. The constructor should by default initialize the artist's name to "None" and the years of birth and death to 0. Define the Artwork class in Artwork.py with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT