0

So, I need to find a way to create a way for the computer to ask for input, for this example lets make it 1 and 2. 1 for yes and 2 for no.

int yes;
int no;

cout << Do you like orange chicken?" << endl;
cout << endl << "1 - Yes" << endl;
cout << "2 - No\n- ";

So my question is, if the user enters K or a char value, how do i get it to ask again? I've been trying to do this inside a do while loop and through a switch statement. I tried in an IF ELSE statement but i couldn't get CIN to register. I know you have to clear CIN after the loop with...

cin.clear();

Then i can get it to ask it again if what the wrong input the user put in was an int value. When the user enters a Char value, every time it goes into a infinite loop...

So if you could be so kind as to post some code that shows me how to do it and explain it that would be great. Thanks =)

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Khoanyneosr
-1
#include <iostream>

int main ()  {
   static const char y='y';
   static const char n='n';

   char answ;
   bool rep=1;


   while (rep)  {
      std::cin >> answ;      
      switch (answ)  {
         case y:
            std::cout << "ok";
            rep=0;
            break;
         case n:
            std::cout << "no";
            rep=0;
            break;
         default:
            rep=1;
            break;
       }
    }
 }
Votes + Comments
One more post with full working code and you will get an infraction
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.