#include <iostream>
#include <cmath>
int main()
{
usingnamespace std;
int Answer;
int Count = 5;
int Win=0;
while(Count <=5 && Count>=0 && Win==0){
cout << endl << endl;
cout << "Tries left: " << Count << endl;
cout << "How old is Obama? ";
cin >> Answer;
if (Answer==50){
cout << "Yes, you're correct! Nice guess.";
Win = 1;}
elseif (Answer<50 && Answer==40){
cout << "A tiny bit too low.";
Count--;}
elseif (Answer<40 && Answer>=0){
cout << "That's way too low!";
Count--;}
elseif (Answer>50 && Answer<=60){
cout << "Kind of high ...";
Count--;}
elseif (Answer>60 && Answer<100){
cout << "That's too high!";
Count--;}
elseif (Answer<0){
cout << "So you're saying he's not even born yet ... ?";
Count--;}
elseif (Answer>100){
cout << "Are you crazy?! That's way too high!";
Count--;}
else {
cout << "Enter a number!";
Count--;}
}
if (Win==1 && Count==5){
cout << endl << "---" << endl << "With one try?! You cheated!";}
elseif (Win==1 && Count!=5){
cout << endl << "---" << endl << "You win! Yay!";}
else {
cout << endl << "---" << endl << "You lose! Haha";}
cout << endl;
cin.ignore();
cin.get();
return 0;
}
I'm having trouble with lines 44 to 46. Basically, I want it so that if the user enters a letter or something, it says to enter a number. Whenever I enter a letter, the program repeats like 5 times then closes.
I'm assuming this is because the Answer variable is an integer ? And you can't enter letters into integers ? I need help! I really have no idea what to do.
Indeed, you break your cin when it expects a number and gets a non-number.
You need to do this check before entering the if statement:
1 2 3 4 5 6 7 8 9
int x;
cout << "Please enter a number: ";
while (!(cin >> x)) { // extract and check for a broken cin
cin.clear(); // reset your cin object
cin.ignore(80, '\n'); // clear the buffer
cout << "Numbers only please, try again: ";
}
cin.ignore(80, '\n'); // incase the first entry was actually a number
It's also a handy function :).
The if statement could be made a little better, consider this:
1 2 3
if (x >= 100);
elseif (x >= 50);
else;
In this situation, the else if will only be reached if x is less than 100. The else will only be reached if x is less than 50. I guess what I am saying is that although 50 is a special number in your case, it doesn't make total sense to have it first in the conditional.
~Perhaps Obama's age should come from ctime, that way your program will still work next year~
I'm still a total newbie and I honestly barely understood what you just said. I've changed it so that it works with my program and its perfect!. I'm going to research into this so I can try and get my head around it.
As for the if and else statements, yeah, that's a great idea!
As for "ctime", no idea how to use it ... I'll look into it.
cin.ignore(80, '\n'); // incase the first entry was actually a number
Also, what does this do?
I can tell that you want to ignore something, but why is 80 there?
I have a feeling that it has something to do with a buffer =S
I had this problem a while ago and quickly got in way over my head. This looks useful, but i've never seen the cin.something commands before.