Solved

Break statement question.

Posted on 2004-09-25
3
248 Views
Last Modified: 2011-09-20
I have a break statement that I have wrote (see below) What would the correct synyax(if it is possible) to add one more statement that is a catch all for all other entries. - I tried adding a while/do statement but that idea doesnt seem to work. Should I be including this in the switch statement or before? The while staement that I attemped is posted below too. Please advise. (Prefer comments and suggestions as opposed to writting the code for me., ) And as always, Thxs much!!

float Wages::assignHourlyRate()
{
      cout << "Enter classification: ";
      cin  >> classification;
      classification = toupper(classification);  ///I put the while statement here///
         switch (classification)
      {      
      case 'A':rate = 10.25;break;
      case 'B':rate = 8.5;break;
      case 'C':rate = 6.3;break;
      case 'D':rate = 4.65;break;
      }
      system ("Pause");
      return 0;
}
while
      (classification != 'A'||classification != 'B'||classification != 'C'||classification != 'D')
            do
      cin  >> classification;                        
0
Comment
Question by:bananaamy
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12151807
You'd add a 'default:' statement, e.g.

float Wages::assignHourlyRate()
{
    bool bClassificationOK= false;
    cout << "Enter classification: ";

    while ( !bClassificationOK) {
    cin  >> classification;
    classification = toupper(classification);
    bClassificationOK = true; //assume the best
       switch (classification)
    {    
     case 'A':rate = 10.25;break;
    case 'B':rate = 8.5;break;
    case 'C':rate = 6.3;break;
    case 'D':rate = 4.65;break;
    default:  bClassificationOK = false; break; // reset if unknown
    }
    } // end while
    system ("Pause");
    return 0;
}
0
 
LVL 1

Accepted Solution

by:
rghome earned 500 total points
ID: 12151879
In your original solution, you made the common mistake of using OR (||) in your while statement instead of AND (&&)

You wrote:

while  (classification != 'A'||classification != 'B'||classification != 'C'||classification != 'D')

But what if classification = 'A' ? The first OR will be false, but the second will be true because 'A' != 'B'.

You should have written:

while  (classification != 'A' && classification != 'B' && classification != 'C' && classification != 'D')
0
 
LVL 86

Expert Comment

by:jkr
ID: 12151961
Um, and what about the 'break;' statement thing?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

730 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