Solved

How to make a Switch Exit

Posted on 2002-05-14
5
190 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

790 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