Number Guesser

Hi. I'm trying to write a program that asks the user to guess Obama's age and gives responses depending on the answer. Here is the code:

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
#include <iostream>
#include <cmath>

int main()
{
  using namespace 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;}
       else if (Answer<50 && Answer==40){
                   cout << "A tiny bit too low.";
                   Count--;}
       else if (Answer<40 && Answer>=0){
                   cout << "That's way too low!";
                   Count--;}
       else if (Answer>50 && Answer<=60){
                   cout << "Kind of high ...";
                   Count--;}
       else if (Answer>60 && Answer<100){
                   cout << "That's too high!";
                   Count--;}
       else if (Answer<0){
                   cout << "So you're saying he's not even born yet ... ?";
                   Count--;}
       else if (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!";}
  else if (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.
Last edited on
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);
else if (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~
Last edited on
Thank you so much for the reply!

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.

Thank you again ! :)
clear the buffer


What's a buffer?

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.
The 'buffer' is the input still in the stream which hasn't been extracted.

80 is the maximum number of characters to ignore if '\n' isn't reached.

I'm sure you'd benefit from the following the link in the post above yours, Fumbles.
Topic archived. No new replies allowed.