Solved

Break statement question.

Posted on 2004-09-25
3
244 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
Comment Utility
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
Comment Utility
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
Comment Utility
Um, and what about the 'break;' statement thing?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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 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.

771 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

14 Experts available now in Live!

Get 1:1 Help Now