Solved

Break statement question.

Posted on 2004-09-25
3
250 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

691 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