944,092 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 8896
  • C++ RSS
0

Random Number Guessing Game

by on Jun 29th, 2005
This is a program I created a couple of weeks ago for class using DevC++ compiler. THis program allows the user 20 tries to guess a number between 1 and 100. It keeps track of wins and losses and offfers the option to continue or quit, displaying game stats.
C++ Code Snippet (Toggle Plain Text)
  1. //Kerri Byrd
  2. //May 28, 2005
  3. //C9A4P447
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <cstdlib>
  8. #include <cctype>
  9. #include <ctime>
  10. #include <conio.h>
  11.  
  12. using namespace std;
  13.  
  14. int main ()
  15. {
  16. int wins = 0;
  17. int losses = 0;
  18. int tries = 0;
  19. int guess;
  20. unsigned int number;
  21. char playAgain = 'Y';
  22.  
  23. srand((unsigned)time(NULL));
  24. number = rand() % 101;
  25.  
  26. while (toupper(playAgain) == 'Y')
  27. {
  28. while (tries < 21)
  29. {
  30. cout << "Enter a number between 0 and 100: ";
  31. cin >> guess;
  32. if (guess == number)
  33. {
  34. wins = wins + 1;
  35. break;
  36. }
  37. else
  38. tries = tries + 1;
  39. }
  40. if (tries > 20)
  41. {
  42. losses = losses + 1;
  43. cout << "Try Again ";
  44. }
  45. else
  46. cout << "You won! ";
  47. cout << "Want to play again?";
  48. cin >> playAgain;
  49. }
  50.  
  51. cout << "Your total wins: " << wins;
  52.  
  53. cout << " Your total losses: " << losses;
  54.  
  55. getch();
  56. return 0;
  57. }
Comments on this Code Snippet
Jun 29th, 2005
0

Re: Random Number Guessing Game

If I can go back and get it to work, I will edit and add some code that will make it a bit more interesting.....
Newbie Poster
kerribyrd is offline Offline
7 posts
since Jun 2005
Nov 24th, 2006
0

Re: Random Number Guessing Game

You may want to take out the non-standard functions and headers (getch(), etc).
Junior Poster
manutd is offline Offline
100 posts
since Nov 2006
Message:
Previous Thread in C++ Forum Timeline: Program 2 copy log file frm database
Next Thread in C++ Forum Timeline: Does constructor create the object ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC