This program works, but not as i want.

I wanted to create a simple program that ask you simple yes/no questions. This is what i managed to get with my limited knowledge.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
using namespace std;
int main()
{
      char Answer;
      start:
      cout << "Do you want a Dinosaur?(1=Yes/0=No):";
      cin >> Answer;
      if (Answer == '1') 
      {
          cout << "Sorry, you can't have one.\n";
                  }
      else if (Answer == '0')
      {
          cout << "Why not!? Dinosaurs are awesome!\n";
          goto start;       
          }
      else
      {
          cout << "That's not an answer, try again.\n";
          goto start;                    
          }
      system("pause");
      }


The issue comes when you type more than 1 character, if you type for expample 15 It first reads the 1 and forgets about the 5, but if you type 23, it automatically runs the program again with the answer being 2 and then 3. How can i make the program to only read the first number, and only the first number?

Thanks beforehand.
I believe that it relates to using char as a data type. Try using int or string and it should work the way you want it.

// int answer
//or
// string answer

//int answser:"

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
  #include <iostream>
using namespace std;
int main()
{
      int answer;
      start:
      cout << "Do you want a Dinosaur?(1=Yes/0=No):";
      cin >> answer;
      if (answer == 1) 
      {
          cout << "Sorry, you can't have one.\n";
                  }
      else if (answer == 0)
      {
          cout << "Why not!? Dinosaurs are awesome!\n";
          goto start;       
          }
      else
      {
          cout << "That's not an answer, try again.\n";
          goto start;                    
          }
      system("pause");
      }
Last edited on
Dont use goto. Use loops.
You can use char only and always check the length of string by strlen function...
If it is more than 1 character long simply ask user to type again...
Last edited on
Topic archived. No new replies allowed.