[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Break statement question.

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
bananaamy
Asked:
bananaamy
  • 2
1 Solution
 
jkrCommented:
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
 
rghomeCommented:
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
 
jkrCommented:
Um, and what about the 'break;' statement thing?
0

Featured Post

Independent Software Vendors: 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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now