Game Programming



Visit Base Classics - Gaming Chairs

 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [C++]Guess The Number Game - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

Posts: 40
Status: Offline
Gender: Male
Joined:  
pm

Reputation: 0%

[C++]Guess The Number Game (11th Jan 08 at 1:51am UTC)
Here is a simple text based game i made quite a few years ago you have to guess the number and if you get it write you win and if you get it wrong you lose

Code:
 
  1. // Number Guessing Game
  2. // Applies the srand() function and loops.
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. srand(time(0));
  13. int randomNumber = rand() % 50 + 1; // generates random number between 1 and 50
  14. int guess = 0;
  15.  
  16. cout << "\tThe Number Guessing Game\n\n"; // Title of program.
  17.  
  18. do
  19. {
  20. cout << "Enter your guess (#1-50): ";
  21. cin >> guess; // put variable into 'guess'
  22.  
  23. if (guess < randomNumber) // If guess is < than the random number, display
  24. cout << "Your guess was too low\n\n";
  25.  
  26. if (guess > randomNumber) // If guess is > than the random number, display
  27. cout << "Your guess was too high\n\n";
  28.  
  29. } while (guess != randomNumber); // While guess is not equal to the random number
  30.  
  31. cout << "\nCongratulations! You've guessed the number.\n\n";
  32. std::cin.get();
  33.  
  34. return 0;
  35. }
  36.  
  37.  
 
 Printable View

All times are GMT+0 :: The current time is 7:53am
Page generated in 4.543 seconds
Purchase Ad-Free Credits
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums