Solved

How to make a Switch Exit

Posted on 2002-05-14
5
188 Views
Last Modified: 2010-04-02
A Switch.

How do I make it that if someone enters anything else other that an integer (1,2,3 or 4) into this switch, that the program then displays a message "Try again" and then displays the menu over again for them to choose from.

At the moment whenever I enter an non integer value it loops constantly, with the default value displaying.

int chioce;
   do
   {
     cin >> choice;

      switch (choice)
      {
         case 1:
           flag=true;
           break;
         case 2:
           flag=true;
           break;
         case 3:
           flag=true;
           break;
         case 4:
           flag=true;
           break
         default:
            cout << "Not a Valid Choice" << endl;
    }while (flag !=true);

Thanks
0
Comment
Question by:dsteers
5 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 100 total points
ID: 7008082
Hi dsteers,

you could do it somehow like this by simply using char instad of int:
char chioce;
do
{
 cin >> choice;

 switch (choice)
 {
  case '1':
   flag=true;
   break;
  case '2':
   flag=true;
   break;
  case '3':
   flag=true;
   break;
  case '4':
   flag=true;
   break
  default:
   cout << "Not a Valid Choice" << endl;
 }
}while (flag !=true);

hope that helps,

ZOPPO
0
 
LVL 22

Expert Comment

by:ambience
ID: 7008158
default:
  cout << "Not a Valid Choice" << endl;
  cin.clear ();
  cin.ignore (INT_MAX, '\n');


for INT_MAX include limits.h
0
 
LVL 6

Expert Comment

by:snoegler
ID: 7008289
Where do you set 'flag' to false?

int chioce;
  do
  {
    cin >> choice;
    flag = false; // <-- i miss this here

     switch (choice)
     {
        case 1:
          flag=true;
          break;
        case 2:
          flag=true;
          break;
        case 3:
          flag=true;
          break;
        case 4:
          flag=true;
          break
        default:
           cout << "Not a Valid Choice" << endl;
   }while (flag !=true);
0
 

Author Comment

by:dsteers
ID: 7008399
Thanks Zoppo :-)
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 7008449
You're welcome,

have a nice day,

regards,

ZOPPO
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now