944,131 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5616
  • C++ RSS
Mar 20th, 2006
0

C++ Loop Question Here

Expand Post »
Hello,
I have this program here>> it's a random guessing number game. It works great the only thing I can't figure out, is how to display the best score at the end of the Loops. The outer loop is suppose to keep track of the number of games played ( number of times the loop is iterated) and the best score. So basically each time the inner loop is done, it displays a score. At the end of the program, if the user selects 'N' to stop the loop or rather end the game, the best score will be displayed or (the least number of times it took the user to guess the number) CAN ANYONE HELP PLEASE????

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int main ( )
  8. {
  9. int bestscore = 0;
  10. int games = 0;
  11. int guess;
  12. int score = 0;
  13. char answer;
  14. srand (time (0));
  15.  
  16. do
  17. {
  18. games ++;
  19.  
  20. int random = rand( );
  21. int num = ((random % 100) + 1);
  22.  
  23. do
  24. {
  25. score ++;
  26.  
  27. cout << "Guess a number between 1 and 100: ";
  28. cin >> guess;
  29.  
  30. if (guess > num)
  31. {
  32. cout << "The number is lower. Try Again!!\n\n";
  33. continue;
  34. }
  35.  
  36. if (guess < num)
  37. {
  38. cout << "The number is higher. Try Again!!\n\n";
  39. continue;
  40. }
  41.  
  42. else if (guess = num)
  43. {
  44. cout << "You've guessed correctly!\n\n";
  45. break;
  46. }
  47.  
  48. }while (guess != num);
  49.  
  50. cout << "Score: "<< score << endl <<endl;
  51. cout << "Would you like to try again? (Y/N): ";
  52. cin >> answer;
  53. cout << endl;
  54.  
  55. }while(answer == 'Y' || answer == 'y');
  56.  
  57. cout << "Number of games played: " << games << endl;
  58. cout << "Best score: " << bestscore << endl;
  59.  
  60. return 0;
  61. }
Last edited by Narue; Mar 21st, 2006 at 9:33 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Duke_0064 is offline Offline
6 posts
since Mar 2006
Mar 21st, 2006
0

Re: C++ Loop Question Here

Well the idea is simple.

It's akin to finding the smallest integer within a set of elements:
How do you think you might do that kiddo?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 21st, 2006
0

Re: C++ Loop Question Here

@Duke_0064
try with this code, hope your problem is solved.
C++ Syntax (Toggle Plain Text)
  1. // #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int main ( )
  8. {
  9. unsigned int bestscore = 0xffffffff;
  10. int games = 0;
  11. int guess;
  12. int score = 0;
  13. char answer;
  14. srand (time (0));
  15.  
  16. do
  17. {
  18. score = 0;
  19. games ++;
  20. int random = rand( );
  21. int num = ((random % 100) + 1);
  22.  
  23. do
  24. {
  25. score ++;
  26. cout << "Guess a number between 1 and 100: ";
  27. cin >> guess;
  28.  
  29. if (guess > num)
  30. {
  31. cout << "The number is lower. Try Again!!\n\n";
  32. continue;
  33. }
  34.  
  35. if (guess < num)
  36. {
  37. cout << "The number is higher. Try Again!!\n\n";
  38. continue;
  39. }
  40.  
  41. else if (guess == num)
  42. {
  43. cout << "You've guessed correctly!\n\n";
  44. break;
  45. }
  46.  
  47. }while (guess != num);
  48.  
  49. cout << "Score: "<< score << endl <<endl;
  50. cout << "Would you like to try again? (Y/N): ";
  51. cin >> answer;
  52. cout << endl;
  53. if(score < bestscore) {
  54. bestscore = score ;
  55. }
  56. }while(answer == 'Y');
  57.  
  58. cout << "Number of games played: " << games<< endl;
  59. cout << "Best score: " << bestscore << endl;
  60.  
  61. return 0;
  62. }
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: using 5 different variables
Next Thread in C++ Forum Timeline: selction sort linked list





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


Follow us on Twitter


© 2011 DaniWeb® LLC