Solved

How to make a Switch Exit

Posted on 2002-05-14
5
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

696 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