BHXSpecter

More than one way to code!

Nov 10th, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. /////////////////MY NUMBER GUESSING CODE FOLLOWED BY CS BOOK CODE////////
  2.  
  3. // number guessing
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     int guess, noOfGuess = 0, number;
  13.     number = (rand() + time(0)) % 100;
  14.        
  15.     while (guess != number && noOfGuess != 5)
  16.     {
  17.         cout << "Guess the number: ";
  18.         cin >> guess;
  19.         cout << endl;
  20.        
  21.         if (guess < number)
  22.         {
  23.             cout << "Guess too low!" << endl;
  24.             noOfGuess++;
  25.         }
  26.         else if (guess > number)
  27.         {
  28.             cout << "Guess too high!" << endl;
  29.             noOfGuess++;
  30.         }
  31.         else
  32.         {
  33.             cout << "You guessed it! The number is: " << number << endl;
  34.         }
  35.     }
  36.    
  37.     if (noOfGuess == 5)
  38.         cout << "You lose, too many guesses! The number is: " << number << endl;
  39.    
  40.     return 0;
  41. }
  42.  
  43.  
  44. ////??????????????? BOOKS NUM GUESS????????????????????????////
  45.  
  46. // Flag-controlled while loop.
  47. // number guessing game
  48.  
  49. #include <iostream>
  50. #include <cstdlib>
  51. #include <ctime>
  52.  
  53. using namespace std;
  54.  
  55. int main()
  56. {
  57.     int num;
  58.     int guess;
  59.     int noOfGuesses;
  60.     bool done;
  61.  
  62.     num = (rand() + time(0)) % 100;
  63.  
  64.     noOfGuesses = 0;
  65.  
  66.     done = false;
  67.  
  68.     while ((noOfGuesses < 5) && (!done))
  69.     {
  70.         cout << "Enter an integer greater than or equal to 0 and less than 100: ";
  71.         cin >> guess;
  72.         cout << endl;
  73.         noOfGuesses++;
  74.  
  75.         if (guess == num)
  76.         {
  77.             cout << "Winner! You guessed the correct number." << endl;
  78.             done = true;
  79.         }
  80.         else if (guess < num)
  81.             cout << "Your guess is lower than the number.\nGuess again!" << endl;
  82.         else
  83.             cout << "Your guess is higher than the number.\nGuess again!" << endl;
  84.     }
  85.  
  86.     if (!done)
  87.         cout << "You lose! The correct number is " << num << endl;
  88.  
  89.     return 0;
  90. }
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment