Solved

escaping from a switch/loop

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

16 Experts available now in Live!

Get 1:1 Help Now