I'm trying to make a program where the user guesses a predetermined number in a program with only 10 tries before the program ends. I can't figure out how to have the code end once the user runs out of tries, can anyone help me? Really stumped on this.
#include <iostream>
usingnamespace std;
int main(int argc, constchar * argv[]) {
int myAge = 15;
int guess;
int guessCount = (10);
cout << "Hi! Lets play a game where you guess my age. Make any guess between 0 and 100\n";
while (guess != myAge){
if (guess != myAge) {
guessCount = guessCount - (1);
cin >> guess;
if (guess > 15){
cout << "Lower! You have " << guessCount << " guesses remaining";
if (guess > 100) {
cout << "\nThats just preposterous. Of course I'm not that old. You have " << guessCount << " guesses remaining\n";
}
}
if (guess < 15){
cout << "\nHigher! You have " << guessCount <<" guesses remaining.\n";
if (guess < 0){
cout <<"\nThat's just preposterous. Of course my age isn't a negative number idiot. You have " << guessCount <<" guesses remaining\n";
}
}
elseif (guess == myAge) {
cout << "Good job";
cout << endl;
}
elseif (guessCount == 0) {
cout << "You lost. Fool.";
cout << endl;
}
}
}
return 0;}
please dont help me with optimizing the code or anything like that. I just need help with the last bit of code
You can add a break; statement after the "you lost" line. This immediately breaks out of the while loop.
Or, make the looping condition be: while (guess != myAge && guessCount > 0)
Also, your guess variable is uninitialized. You should initialize to some invalid value like -1.
Also also, your indentation is inconsistent, and this makes your code hard to read and makes it prone to errors.
you have the right idea: decrement a counter (x--; is the same as x = x -1).
the problem is your if and else if chains are not fully connected, so its not always going down a path that can end the program. This is easier to see if you align your braces rather than use book style. Book style is valid, but it hides some alignment errors.
you want to tie all the logic together.
something that flows like this logic:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while(guesscount && guess!= age)
{
if (invalid) //negative, and possibly > 130 or something
invalid stuff;
elseif (win)
winstuff;
elseif(lower)
lowerstuff;
elseif(higher)
higherstuff;
//***********************************
guesscount--; //not under any condition
}//end loop
-> if guess count is zero here, they lost.
->else they won.
#include <iostream>
int main() {
constexprint myAge {15};
constexprint MaxGuess {10};
int guess {};
std::cout << "Hi! Lets play a game where you guess my age.\n";
for (int guessCount {MaxGuess}; guessCount && guess != myAge; --guessCount) {
std::cout << "\nYou have " << guessCount << " attempts remaining. Make any guess between 0 and 100: ";
std::cin >> guess;
if (guess > myAge)
if (guess > 100)
std::cout << "That's just preposterous. Of course I'm not that old.\n";
else
std::cout << "Lower!\n";
elseif (guess < 0)
std::cout << "That's just preposterous. Of course my age isn't a negative number idiot.\n";
elseif (guess < myAge)
std::cout << "Higher!\n";
}
if (guess != myAge)
std::cout << "\nYou lost. Fool.\n";
else
std::cout << "\nGood job.\n";
}