C++ Loop Question Here

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 6
Reputation: Duke_0064 is an unknown quantity at this point 
Solved Threads: 0
Duke_0064 Duke_0064 is offline Offline
Newbie Poster

C++ Loop Question Here

 
0
  #1
Mar 20th, 2006
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????

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: C++ Loop Question Here

 
0
  #2
Mar 21st, 2006
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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: C++ Loop Question Here

 
0
  #3
Mar 21st, 2006
@Duke_0064
try with this code, hope your problem is solved.
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC