Kaktusen

Kaktus - Guess the number

Mar 27th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <stdlib.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void game();
  9.  
  10. int main() {
  11.     string menu;
  12.     cout << "Welcome to 'Guess a number'" << endl;
  13.     cout << "Write 'Start' to start the game!" << endl;
  14.     cin >> menu;
  15.  
  16.     if(menu == "start" || menu == "Start") {
  17.         game();
  18.     } else {
  19.         return 0;
  20.     }
  21. }
  22.  
  23. void game() {
  24.     srand(time(NULL));
  25.     int high;
  26.     int low;
  27.     int guess;
  28.     int number;
  29.     bool gameLoop = true;
  30.  
  31.     cout << "Write down the highest number you want to guess: ";
  32.     cin >> high;
  33.     cout << endl;
  34.     cout << "Write down the lowest number you want to guess: ";
  35.     cin >> low;
  36.     cout << endl;
  37.  
  38.     guess = rand() % high + low;
  39.  
  40.     while(gameLoop) {
  41.         cout << "Guess a number: ";
  42.         cin >> number;
  43.  
  44.         if(number > guess) {
  45.             cout << endl << "<" << endl;
  46.         } else if(number< guess) {
  47.             cout << endl << ">" << endl;
  48.         } else if(number == guess) {
  49.             gameLoop = false;
  50.             cout << "You won!" << endl;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment