Random Number Guessing Game
Jan 21, 2015 at 6:59pm UTC
Write your question here.
I am trying to get the user to enter the min number allowed and the max number allowed for the random number to generate between. I am also trying to get it to allow the user to enter the number of tries allowed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
Put the code you need help with here.
#include "stdafx.h"
#include "iostream"
#include "string"
#include <ctime>
#include <stdlib.h>
#include <time.h>
using namespace std;
int ChooseRandomNumber()
return << rand()(maxBound - minBound0) + minBound;
int main(void ) {
srand(time(0));
while (true ) {
string minBoun;
string maxBound;
int number = rand() %100 +1;
int guess;
int tries = 0;
char answer;
while (true ) {
cout << "Enter a min number: " << minBound << endl;
getline(cin, minBound);
cout << "Enter a max number: " << maxBound << endl;
getline(cin, maxBound);
if (guess > number) {
cout << "Too high! Try again.\n" ;
} else if (guess < number) {
cout << "Too low! Try again.\n" ;
} else {
break ;
}
tries++;
}
if (tries >= 20) {
cout << "You ran out of tries!\n\n" ;
} else {
cout<<"Congratulations!! " << std::endl;
cout<<"You got the right number in " << tries << " tries!\n" ;
}
while (true ) {
cout << "Would you like to play again (Y/N)? " ;
cin >> answer;
cin.ignore();
if (answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y' ) {
break ;
} else {
cout << "Please enter \'Y\' or \'N\'...\n" ;
}
}
if (answer == 'n' || answer == 'N' ) {
cout << "Thank you for playing!" ;
break ;
} else {
cout << "\n\n\n" ;
}
}
cout << "\n\nEnter anything to exit. . . " ;
cin.ignore();
return 0;
}
Jan 21, 2015 at 7:54pm UTC
- minBound and maxBound should be ints instead of strings.
- Enter the minBound and maxBound before line 21. Consider adding error checking here.
- Replace line 21 with code to compute a random number between minBound and maxBound.
Jan 21, 2015 at 11:38pm UTC
Thank you, I will try that when I am back in school friday.
Topic archived. No new replies allowed.