Question

mathTutor Write a program that selects two random numbers -20 to 20. The two numbers would...

mathTutor
Write a program that selects two random numbers -20 to 20. The two numbers would get displayed with "+" between them. The user should enter the sum of the two numbers.
The program should print "Incorrect" if the user enters a wrong answer and re-prompts the user to re-enter the answer again. Limit the number of incorrect tries to 3. After 3 incorrect tries print the correct answer. The program should print a message like "Correct!" if the user enters the right answer.  

The program would then display "Would you like another problem y/n? ". If user selects "y" or "Y" you show them another problem to be solved.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <iostream>

#include <stdlib.h> /* srand, rand */

#include <time.h> /* time */

using namespace std;

int main()

{

int minRange = -20,

maxRange = 20,

num1,

num2,

result,

answer,

tries = 0;

char choice;

srand(time(NULL));

do

{

tries = 0;

// get random numbers

num1 = minRange + (rand() % (maxRange - minRange + 1));

num2 = minRange + (rand() % (maxRange - minRange + 1));

answer = num1 + num2;

cout << "What the sum of " << num1 << " + " << num2 << " = ";

// read result

cin >> result;

do

{

tries++;

if (result == answer)

{

cout << "correct\n";

break;

}

else if (tries == 3)

{

cout << "Correct answer is : " << answer << endl;

break;

}

cout << "Incorrect, Try again : ";

cin >> result;

} while (tries != 3); // if incorrect then loop again

cout << "\nWould you like another problem y/n? : ";

cin >> choice;

} while (choice == 'Y' || choice == 'y');

}

Output:

Add a comment
Know the answer?
Add Answer to:
mathTutor Write a program that selects two random numbers -20 to 20. The two numbers would...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • mathTutor Write a program that selects two random numbers -20 to 20. The two numbers would...

    mathTutor Write a program that selects two random numbers -20 to 20. The two numbers would get displayed with "+" between them. The user should enter the sum of the two numbers. The program should print "Incorrect" if the user enters a wrong answer and re-prompts the user to re-enter the answer again. Limit the number of incorrect tries to 3. After 3 incorrect tries print the correct answer. The program should print a message like "Correct!" if the user...

  • Write a program which gives an easy mathematics quiz. The program should display two random numbers...

    Write a program which gives an easy mathematics quiz. The program should display two random numbers which are to be added together, like this: 117 + 213 ----- The program should ask the user to enter their answer. If the answer is correct, the user should be congratulated. If the answer is wrong, the right answer should be displayed and the user should be scolded. Don't forget to: Generate random numbers Ask the user if they want to be tested...

  • Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the...

    Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message such as "You win!" and terminates. Otherwise, the program checks and tell the user whether the guessed number is less or greater than the target number and then prompts him for another try. However, the program gives the user five tries only to guess the correct...

  • Write a program which: - prompts the user for 2 numerical inputs - stores their two...

    Write a program which: - prompts the user for 2 numerical inputs - stores their two inputs in variables as floats - Use a while in loop to ask a user for a valid operation: (if the user's input operation isn't one of those listed above, instead print a message telling them so, and continue asking for the valid operation) - prompts the user for an arithmetic operation ( + - * / %) - stores the user's input as...

  • Write a MIPS math quiz program in MARS. The program should start with a friendly user...

    Write a MIPS math quiz program in MARS. The program should start with a friendly user greeting. From there, it should generate a random arithmetic problem. Your program will need to generate three random things: the first and second operand and the operator to use. Your program should generate random positive integers no greater than 20 for the operands. The possible operators are +, -, * and / (division). The user should be prompted for an answer to the problem....

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4...

  • c++ Write a program that prompts the user to enter a length in feet and then...

    c++ Write a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again.

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • Using C++ please create a program for a first grader to practice subtractions. The program randomly...

    Using C++ please create a program for a first grader to practice subtractions. The program randomly generates two single-digit integers number1 and number2. Ask the user to input the difference of the two numbers with always the greater number as the first operand. If the user inputs wrong answer, print a message, “Your answer is wrong. The correct answer is ” with the correct answer. Otherwise print “Your answer is correct.” Hint: 1. Check if number1 < number2, otherwise swap...

  • Write a MIPS assembly program to calculate nCr. The program should accept the numbers n and...

    Write a MIPS assembly program to calculate nCr. The program should accept the numbers n and r from the user. The program should call the factorial function. nCr = n! / ((n-r)! * r!) If n<r print an error message "n should not be less than r: re-enter the values." and get the inputs n and r from the user.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT