| | |
C++ Loop Question Here
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 0
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????
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)
#include <iostream> #include <cstdlib> #include <cmath> #include <ctime> using namespace std; int main ( ) { int bestscore = 0; int games = 0; int guess; int score = 0; char answer; srand (time (0)); do { games ++; int random = rand( ); int num = ((random % 100) + 1); do { score ++; cout << "Guess a number between 1 and 100: "; cin >> guess; if (guess > num) { cout << "The number is lower. Try Again!!\n\n"; continue; } if (guess < num) { cout << "The number is higher. Try Again!!\n\n"; continue; } else if (guess = num) { cout << "You've guessed correctly!\n\n"; break; } }while (guess != num); cout << "Score: "<< score << endl <<endl; cout << "Would you like to try again? (Y/N): "; cin >> answer; cout << endl; }while(answer == 'Y' || answer == 'y'); cout << "Number of games played: " << games << endl; cout << "Best score: " << bestscore << endl; return 0; }
Last edited by Narue; Mar 21st, 2006 at 9:33 am.
@Duke_0064
try with this code, hope your problem is solved.
try with this code, hope your problem is solved.
C++ Syntax (Toggle Plain Text)
// #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> using namespace std; int main ( ) { unsigned int bestscore = 0xffffffff; int games = 0; int guess; int score = 0; char answer; srand (time (0)); do { score = 0; games ++; int random = rand( ); int num = ((random % 100) + 1); do { score ++; cout << "Guess a number between 1 and 100: "; cin >> guess; if (guess > num) { cout << "The number is lower. Try Again!!\n\n"; continue; } if (guess < num) { cout << "The number is higher. Try Again!!\n\n"; continue; } else if (guess == num) { cout << "You've guessed correctly!\n\n"; break; } }while (guess != num); cout << "Score: "<< score << endl <<endl; cout << "Would you like to try again? (Y/N): "; cin >> answer; cout << endl; if(score < bestscore) { bestscore = score ; } }while(answer == 'Y'); cout << "Number of games played: " << games<< endl; cout << "Best score: " << bestscore << endl; return 0; }
![]() |
Similar Threads
- C++ For Loop User Response (C++)
- Simple script loop question (Shell Scripting)
- C++ For Loop Question (C++)
- while loop question (Python)
- how to use loop in this question (C++)
- need help w/for loop (C++)
- I need to re-write this code using a 'while' loop. (Java)
Other Threads in the C++ Forum
- Previous Thread: using 5 different variables
- Next Thread: please explain initializer list.
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






