yes or no question

hey, can anyone tell me how to make a yes or no question??
like do you want more tea?? (y\n)
thanks
You could do this. First you gotta declare what the Y\N will be called (I usually call it response)

char response; //Note it is a char, not int or whatever

Then later you can do stuff like -

1
2
3
4
5
6
7
8
9
10
cout << "Do you want more tea?";
cin >> response;
if (response == 'Y' //not "Y" but only one 'Y')
{
blah blahblah
}
else if (response == 'N')
{
blah blahblah
}
Dear sir,
You can try switch case as follows::
cout<<"Do you wish to continue(Y/N)";
char response;
cin>>response;
switch(rsponse)
{
case 'y':__________
_________
break;
case 'n':__________
_________
break;
default: cout<<"invalid choice";
}

With Regards,
Matanuragi
The best way is:

if (!( a != 'Y' && a != 'y'))

'a' is a variable.

Thanks to jsmith.
Last edited on
Don't forget else! Here is my tip:
create two functions with the same code. You can use line from previous coments. But else in the first function must call the second function and else in second function must call the first function. So if you answer is wrong second function will be called. If you answer is wrong then first function will be called...

Void one(){
if (a=='Y'){//blabla}
else if(a=='N'){//blablabla or leave it empty to continue program}
else {two();}
}
void two(){
if(a=='Y'){bla bla bla}
else if(a=='N'){bla bla bla}
else{one():}
}
int main(){

int a;
cin >>a;
one();

return 0;
}
This all looks a bit more complicated than it needs to be. All the answers given will work fine, but keep things simple.

For lexigraphical input (where the input is the first letter of words, like "yes" and "no"), you can use toupper() or tolower() to simplify your tests:
1
2
3
4
5
6
7
8
#include <cctype>
#include <iostream>

  char c;
  cout << "Would you like some tea? (Y/N)? ";
  cin >> c;
  if (toupper( c ) == 'Y') give_user_some_tea();


For processing multiple options, a switch statement works nicely:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int menu()
  {
  char c;
  cout << "What would you like to do?\n"
          "  [F]ight\n"
          "  [C]ast spell\n"
          "  Evade and [R]un away\n"
          "  [Q]uit\n";
  cin >> c;
  switch (toupper( c ))
    {
    case 'F':                     return FIGHT;
    case 'C': case 'S':           return CAST_SPELL;
    case 'E': case 'R': case 'A': return EVADE;
    case 'Q':                     return QUIT;
    }
  return UNKNOWN;
  }
(Using the switch like this is particularly useful for foreign languages where a word may begin with a different letter depending on whether it is answered in upper-case or lower-case.)

For handling invalid inputs, using a loop and a string is a more consise option:
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
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

bool dogs_or_cats()  // true for dogs, false for cats
  {
  cout << "Do you prefer dogs or cats? ";
  while (true)
    {
    string s;
    cin >> ws;   // skip any leading whitespace
    getline( cin, s );
    if (s.empty()) continue;
    switch (toupper( s[ 0 ] ))
      {
      case 'D': return true;
      case 'C': return false;
      }
    cout << "Yes, but this is only about dogs or cats. Which do you prefer? ";
    }
  }

int main()
  {
  if (dogs_or_cats())
    cout << "You must be an energetic, fun-loving individual!\n";
  else
    cout << "You must be an empathetic, caring individual!\n";
  return 0;
  }


As you can see, there are many options... use the one that best suits your needs.

Hope this helps.
Previous example was terrible but i made this:
<code>
Int looper=0;

while(looper==0){
cout <<"do you want...(y/n)\n";

char a;
cin >>a;
if a(a=='y'){
cout <<"put here what you want...";looper=1;}
else if(a=='n'){
cout <<"put here what you want";looper=1;}
else{cout <<"bad answer, try again\n";looper=0;}
}
<code>
so what you think about this?
Note: my example was wrong, not yours :-)
Last edited on
Thanks for all info hehehe
Topic archived. No new replies allowed.