Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <cstdio>
- using namespace std;
- int main()
- {
- char loop; // Decalres loop in the proper scope.
- srand(time(NULL)); // Initializes random number generator.
- int ran1, ran2, result, guess = 120, tries = 0, choice = 0; // Decelares variables for program.
- // guess is set to 11 so the while statement will never be true on first run.
- // Tries is reset to zero for loop.
- bool valid = false;
- const int maxtries = 3; // Sets a constant for the maximum number of tries.
- do //Start program loop
- {
- loop = 'z'; // Resets loop variable.
- guess = 120; // Resets Guess.
- tries = 0; // Resets Tries counter.
- result = ran1 * ran2; // Calculates the results for comparison.
- cout << "Welome to math helper! What difficulty would you like to play at?\n";
- do
- {
- cout << "(1) Hard: 0-10\n(2)Medium: 0-7\n(3)Easy: 0-5\n";
- cin >> choice;
- if (choice == 1)
- {
- ran1 = rand()%11; // Finds first random number between 1 and 10.
- ran2 = rand()%11; // Finds second random number between 1 and 10.
- }
- if (choice == 2)
- {
- ran1 = rand()%8; // Finds first random number between 1 and 10.
- ran2 = rand()%8; // Finds second random number between 1 and 10.
- }
- if (choice == 3)
- {
- ran1 = rand()%5; // Finds first random number between 1 and 10.
- ran2 = rand()%5; // Finds second random number between 1 and 10.
- }
- if ((choice != 1) || (choice != 1) || (choice != 1))
- cout << "Please enter a valid value!\n";
- }
- while ((choice != 1) || (choice != 1) || (choice != 1));
- cout << "Try to solve the problem!\n"; // Provides instruction to the user.
- while ((result != guess) && (tries < maxtries))
- // Starts loop that will run until the user answers correctly or maxtries is exceeded.
- {
- valid = false;
- while (!valid)
- {
- cout << ran1 << " x " << ran2 << " = "; // Displays equation
- cin >> guess;
- valid = true;
- if (cin.peek() != '\n' || cin.fail())
- {
- valid = false;
- cin.clear();
- cin.ignore(100, '\n');
- cout << "Enter a valid integer.\n" << ran1 << " x " << ran2 << " = "; // Displays equationendl;
- }
- }
- }
- tries = tries + 1; // Adds one to the tries counter
- }
- if (result == guess) // Checks to see for the correct answer after loop.
- cout << "You win!\n"; // Tells user they're a winner.
- else // If they are not correct, logic dictates they must be wrong.
- cout << "You lose!\n"; // Tells user they don't math goodly.
- while ((loop != 'n') && (loop != 'N') && (loop != 'Y') && (loop != 'y'))
- {
- cout << "\nDo you want to play again? (Y/N) "; // Asks user if they want to start again.
- cin >> loop; // Stores answer.
- if ((loop != 'n') && (loop != 'N') && (loop != 'Y') && (loop != 'y'))
- cout << "Invalid input. Please try again.\n";
- }
- }
- while ((loop == 'y') || (loop == 'Y')); // If answer was upper or lower case y, the program loops.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement