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
Solved

escaping from a switch/loop

Posted on 2004-10-14
3
244 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
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…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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