Counting the number of guesses?
Apr 22, 2015 at 8:35am UTC
Hi! I took it upon myself to work on the most generic and cliche cpp beginner's program. And i can't figure out how to tell the computer to count the amount guesses the user inputted. The most logical way i thought is to somehow count how many times the while loop has been executed, or am i way off? Any help would be appreciated! <3
YourBroAlex
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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main() {
int guess;
int randomize = rand() % 100 + 1;
bool status;
int numOfTries;
string showanswer;
cout << "Welcome to the random number guessing game!" << endl;
cout << "I'm thinking of a number between 1 and 100, can you guess what it is?" << endl;
while (status == true ) {
cin >> guess;
if (randomize > guess) {
cout << "Too low! Guess again!" << endl;
++numOfTries;
} else if (randomize < guess) {
cout << "Too high! Guess again!" << endl;
++numOfTries;
} if (randomize == guess) {
status = false ;
cout << "Good job! You guessed the right number! You guessed " << numOfTries << " times!" << endl;
}
}
}
Apr 22, 2015 at 8:52am UTC
use proper indentation
http://en.wikipedia.org/wiki/Indent_style
in line 19, you are evaluating
status
but you have not defined its value before.
or, you don't need
status
variable at all. just use
while (true )
and
break ;
when right number is guessed.
Topic archived. No new replies allowed.