Solved

escaping from a switch/loop

Posted on 2004-10-14
3
246 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
[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
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 clear a vector as well as how to detect empty vectors in C++.

687 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