?
Solved

How to make a Switch Exit

Posted on 2002-05-14
5
Medium Priority
?
193 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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

800 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