1.11M Members

Using a switch statement with nested if/else statements

 
0
 

Hi Everyone,
I am taking C++ for the second time and I am understanding it better this time around. I have an assignment that needs a switch (got that, know how to do it), I have put if/else statements inside of the cases. The thing I can't get to work is:
1. If the user enters the incorrect case (i.e. any letter besides a, b, or c) before going to the default it will jump to my next cout statement, then it prompts for the user to re-enter the correct letter, after that it ends. So I need advice on how to get it to stop asking for the hours used after the wrong input, and I need it to continue to run after the correct input is re-entered. I hope that I haven't confused anyone to bad. I have the code wrote, but am not real sure what I can or can't post or even how it is to be posted. Also we are using jGrasp with the cygwin compiler. Thanks.

 
0
 

Post your code using code tags. You can find out how in the FAQ or just about any of the stickies. If you still can't figure it out, post your code anyway and a moderator will tag it for you (just this once though).

 
0
 

i guess your mistake is not using break; after each case... if you don't do this, your switch will run all the way until all the cases are done...

another thing i would advise you to do is use the default until the end of the switch statement, since this way you will not have this kind of problems...

 
0
 

I have all the breaks and I have my default set to prompt the user to re-enter the correct package (letter). I need to read how to post my code, so that I do everything correctly. I apperciate your input. I will try and get the code posted soon.

 
0
 

Here is what I have done. My instructor said that I need to put the package in a seperate loop, but all the ways I have tried do not work. I need for it to continue after the the default info is entered and I need it not to ask for hours used if the user entered the wrong package letter.

#include <iostream>
using namespace std;
 
int main()
{
//variable declarations
char package;
int hours;
double costA = 9.95, costB = 14.95, costC = 19.95;
 
cout << "Which package does the customer use (A, B, or C,)? ";
cin >> package;
cout << "How many hours did the customer use last month? ";
cin >> hours;
 
if ((hours < 0)||(hours > 744))
{
cout << "ERROR!\n"
<< "Enter a number from 0 through 744 for hours used.\n"
<< "Please re-enter hours used: ";
cin >> hours;
}
 
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
 
switch (package)
{
case 'a':
case 'A': 
if (hours <= 10)
costA = 9.95; 
else if (hours > 10)
costA = (2.00 *(hours - 10) + (costA));
cout << "The charges are $" << costA << endl; 
if ((costA > 14.95)||(costA > 19.95))
costB = (1.00 *(hours - 20) + (costB));
costB = costA - costB; 
cout << "By switching to Package B you would save $"
<< costB << endl;
costC = costA - costC;
cout << "By switching to Package C you would save $"
<< costC << endl;
break; 
case 'B':
case 'b': 
if (hours <= 20)
costB = 14.95; 
else if (hours > 20)
costB = (1.00 *(hours - 20) + (costB));
cout << "The charges are $" << costB << endl;
if (costB > 19.95)
costB = costB - costC;
cout << "By switching to Package C you would save $"
<< costB << endl; 
break; 
case 'C':
case 'c': 
if (hours <= 744)
costC = 19.95;
cout << "The charges are $" << costC << endl;
break; 
default:
cout << "Enter only A, B, or C for the package.\n"
<< "Please re-enter package: ";
cin >> package; 
}
 
return 0;
}

By the way, I'm still not sure if I followed the correct guidelines for piosting code. Please advise me if I have done something wrong. Thanks in advance.

 
0
 

you are pretty close to what is posting correctly your code... all you have to do identify the code you are using as c code ([[B]code=c[/B]]), and write your code with the right margins so it is easier to follow...

second, what your teacher means by putting the package in a loop is that, for example, if the user inputs the wrong package once, he will be able to input a wrong package the second time, and get away with murder... this is why you must do a while loop that checks if the user's answer is inside the boundaries of the package...

i recommend you do the same thing with the hours...

by the way... there's no need to do this:

case 'a':
case 'A':...

you can do it this way:

case ('a'||'A'):...

i'm sure narue will correct me if i'm wrong...

 
0
 

>you can do it this way:
>case ('a'||'A'):...
You can, but it won't work like you expect. ('a'||'A') is a boolean expression that evaluates to true. It's identical to:

case 1:
 
0
 

By the way, I'm still not sure if I followed the correct guidelines for piosting code. Please advise me if I have done something wrong. Thanks in advance.

Nope.
Problem 1: You didn't use CODE tags as the background of the input box advises. Also here
Problem 2: Formatting code

i'm sure narue will correct me if i'm wrong...

You're wrong :icon_razz:
If you aren't sure, it only takes a minute or two to test ideas like this one before posting and prevents Narue from having fun at your expense :icon_twisted:

 
0
 

but, it works with what lovetwins wants to do, since it will evaluate if package equals to 'a' or || 'A' ...

 
0
 

You're wrong :icon_razz:
If you aren't sure, it only takes a minute or two to test ideas like this one before posting and prevents Narue from having fun at your expense :icon_twisted:

:-/ I like it when narue makes fun of me... i don't know... maybe i'm a little wacked... but it just adds a little sense of humor to my day...:D

 
0
 

> but, it works with what lovetwins wants to do, since it will evaluate if package equals to 'a' or || 'A'...
No. It evaluates to case 1, so entering 'a' or 'A' would give the message 'Enter only a, b or c for package'.

 
0
 

why?

 
0
 

Because constant-expressions are evaluated in the case construct. True evaluates to 1, false evaluates to 0.

case (i + 1): //flag an error, requires constant expression
    cout << "In error";
    break;
case (3 + 3):
    cout << "In case 6";
    break;
case (3 == 0):
   cout << "In case 0";
   break;
case (3 == 3):
   cout << "in case 1";
   break;
 
0
 

but, if you are evaluating a character, along with another one, say in this case is capital form, can't you do it with an or || expression?

case ('a'||'A')
 
0
 

No, because constant expressions are evaluated, and you end up with the expression 'a' || 'A' getting evaluated. Any conditional expression within the case construct would either evaluate to 0 or 1.

The way the OP had done it is the correct way. Either that, or convert the character accepted to lowercase and keep all lowercase characters in your case constructs.

 
0
 

>but, if you are evaluating a character, along with another one, say in this
>case is capital form, can't you do it with an or || expression?
Um, did you not read the posts that told you (in reply to you!) that you can't do that and why it doesn't work? Because it seems a lot like what we say to you is going in one ear and right out the other.

 
0
 

alright... alright... jeje...

it had been a long time since i got you mad at me huh narue?

 
1
 

>it had been a long time since i got you mad at me huh narue?
If you want to get me mad, keep using jeje. It's a big pet peeve of mine. I used to play a game called X-wing vs. Tie Fighter and was actually one of the better players in the world. There was a club that insisted on speaking spanish (which was rude enough but we let it slide) and always (like, every sentence) used jeje or jaja. It annoyed me so much that I went out of my way to play games against them as much as possible to make sure they got raped on a regular basis. I'm not sure, but I think I was one of the reasons they disbanded.

 
0
 

i'm sorry... please don't stop talking to me... :P

it's just that i'm used to laugh like that... but i'll do my best... okay?

 
0
 

Seems to me, that this is more a a gossip forum, than a help forum. I came here for help and advice, not to waste my time reading all the bickering and snide comments you all had to say to each other. It was a waste of my time. I would like to thank the ones that were actually trying to suggest ideas. I'm appauled that a so called moderator would even be pulled into discussions as such.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article