Solved

How to make a Switch Exit

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

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 30

Expert Comment

by:Zoppo
ID: 7008449
You're welcome,

have a nice day,

regards,

ZOPPO
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
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.

708 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

18 Experts available now in Live!

Get 1:1 Help Now