Advertisement
Guest User

Code 1

a guest
Sep 19th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cstdio>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.   char loop; // Decalres loop in the proper scope.
  11.   srand(time(NULL)); // Initializes random number generator.
  12.  
  13.     int ran1, ran2, result, guess = 120, tries = 0, choice = 0; // Decelares variables for program.
  14.       // guess is set to 11 so the while statement will never be true on first run.
  15.       // Tries is reset to zero for loop.
  16.     bool valid = false;
  17.     const int maxtries = 3; // Sets a constant for the maximum number of tries.
  18.      
  19.   do //Start program loop
  20.   {
  21.     loop = 'z';  // Resets loop variable.
  22.     guess = 120; // Resets Guess.
  23.     tries = 0; // Resets Tries counter.
  24.     result = ran1 * ran2; // Calculates the results for comparison.
  25.    
  26.     cout << "Welome to math helper! What difficulty would you like to play at?\n";
  27.    
  28.   do
  29.   {
  30.     cout << "(1) Hard: 0-10\n(2)Medium: 0-7\n(3)Easy: 0-5\n";
  31.     cin >> choice;
  32.    
  33.     if (choice == 1)
  34.     {
  35.       ran1 = rand()%11; // Finds first random number between 1 and 10.
  36.       ran2 = rand()%11; // Finds second random number between 1 and 10.
  37.     }
  38.    
  39.     if (choice == 2)
  40.     {
  41.       ran1 = rand()%8; // Finds first random number between 1 and 10.
  42.       ran2 = rand()%8; // Finds second random number between 1 and 10.
  43.     }
  44.    
  45.     if (choice == 3)
  46.     {
  47.       ran1 = rand()%5; // Finds first random number between 1 and 10.
  48.       ran2 = rand()%5; // Finds second random number between 1 and 10.
  49.     }
  50.     if ((choice != 1) || (choice != 1) || (choice != 1))
  51.       cout << "Please enter a valid value!\n";
  52.   }
  53.   while ((choice != 1) || (choice != 1) || (choice != 1));
  54.    
  55.     cout << "Try to solve the problem!\n"; // Provides instruction to the user.
  56.    
  57.     while ((result != guess) && (tries < maxtries))
  58.       // Starts loop that will run until the user answers correctly or maxtries is exceeded.
  59.     {  
  60.     valid = false;
  61.     while (!valid)
  62.     {
  63.       cout << ran1 << " x " << ran2 << " = "; // Displays equation
  64.       cin >> guess;
  65.       valid = true;  
  66.    
  67.       if (cin.peek() != '\n' || cin.fail())
  68.       {
  69.         valid = false;
  70.         cin.clear();
  71.         cin.ignore(100, '\n');
  72.         cout << "Enter a valid integer.\n" << ran1 << " x " << ran2 << " = "; // Displays equationendl;
  73.       }
  74.     }
  75.     }  
  76.     tries = tries + 1; // Adds one to the tries counter
  77.     }
  78.    
  79.     if (result == guess) // Checks to see for the correct answer after loop.
  80.       cout << "You win!\n"; // Tells user they're a winner.
  81.     else // If they are not correct, logic dictates they must be wrong.
  82.       cout << "You lose!\n"; // Tells user they don't math goodly.
  83.  
  84.     while ((loop != 'n') && (loop != 'N') && (loop != 'Y') && (loop != 'y'))
  85.     {
  86.       cout << "\nDo you want to play again? (Y/N) "; // Asks user if they want to start again.
  87.       cin >> loop; // Stores answer.
  88.       if ((loop != 'n') && (loop != 'N') && (loop != 'Y') && (loop != 'y'))
  89.     cout << "Invalid input. Please try again.\n";
  90.     }
  91. }
  92.   while ((loop == 'y') || (loop == 'Y')); // If answer was upper or lower case y, the program loops.
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement