User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,637 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,290 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Jun 29th, 2005
Views: 1,507
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 Syntax | 1 stars
  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. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 2:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC