Solved

escaping from a switch/loop

Posted on 2004-10-14
3
242 Views
Last Modified: 2010-04-02
Hullo,

I've written a very simple switch inside a loop, it's basically :

  do {
  cout << "\n\nYour answer is :   ";
  cin >> letter;
  letter = toupper(letter);
  switch ( letter )
{
  case 'A': cout << "Incorrect.";
  break;
  case 'B': cout << "Correct.";
  break;
 } } while (letter);

It actually has a few more options, but I won't bore you with that . . The thing is, while this is going on, there's nothing the user can do to end the program.

How can I specify something for the user to type in that will let them escape this loop ?
0
Comment
Question by:drivensnow
3 Comments
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 12314658
do {
  cout << "\n\nYour answer is :   ";
  cin >> letter;
  letter = toupper(letter);
  switch ( letter )
{
  case 'A': cout << "Incorrect.";
  break;
  case 'B': cout << "Correct.";
  break;
 }
if(letter == 'Q')
break; // this break will jump out of the loop
 } while (letter);

welcome to www.fruitfruit.com
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 500 total points
ID: 12314730
do {
      cout << "\n\nYour answer is :   ";
      cin >> letter;
      letter = toupper(letter);
      switch ( letter )
      {
            case 'A': cout << "Incorrect.";
            break;
            case 'B': cout << "Correct.";
            break;
            case 'Q': cout << "Bye.";  letter = 0;      
            break;
      }
} while (letter);
0
 

Author Comment

by:drivensnow
ID: 12316013
It works !  :D Thankee.
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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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 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.

919 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