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.

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).

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...

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.

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.

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...

>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:

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:

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

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

> 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'.

why?

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;

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')

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.

>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.

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

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

>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.

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?

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.

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...

this i wrote it as a response to your first post, where the program executed all of the commands even ehen they were in a switch...

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...

this, i wrote it as a response to your second question, answering what your teacher meant when he said that your package needed to be inside a loop...

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

this and almost all that follows is an excelent explanation given by narue and WaltP about why

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

is not the same thing as

case ('a'||'A')

besides... let me remember you that we are solving YOUR problems so i think we need a little respect here... we (specially those who have been here a long long time) might know better than you would about how to handle posts and threads... plus... who doesn't get a little off topic once in a while?

>Seems to me, that this is more a a gossip forum, than a help forum.
I'd disagree, but that's unlikely to change your view. And quite frankly, if you're reacting like a child to something so trivial, I'm not interested in changing your view. You can whine, cry, stamp your feet, hold your breath, and leave Daniweb for all I care. You haven't made enough of an impact for us to even notice that you left.

>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.
Wow, you're a real jerk. I'm sorry we wasted your time, and I guess we'll never be seeing your ungrateful presence around here again. That's a good thing too, because I'm sure you'll be a big baby every time you don't get exactly what you want in exactly the form you want. And I can guarantee that you'll encounter situations worse than a slightly off-topic thread with that attitude.

>I'm appauled that a so called moderator would even be pulled into discussions as such.
I'm constantly appalled that people seem to think that being a moderator means I'm not allowed to participate in regular threads. :icon_rolleyes: This is the single most annoying complaint I see when someone doesn't agree with my actions. "You're a moderator, how can you do that?" Are you guys really so stupid that you can't see the difference between enforcing the rules and being active in the community?

> Seems to me, that this is more a a gossip forum, than a help forum.
You are the only one with this kind of opinion I guess.

> I came here for help and advice.
If you still haven't been able to find those, then you are not cut for programming.

> It was a waste of my time.
You were in need of help. It was actually a waste of our time. This kind of attitude would get you nowhere.

> I would like to thank the ones that were actually trying to suggest ideas.
This kind of attitude would get you everywhere. This is the way it has to be when you ask for help.

You have to bear in mind that no one here is obliged to answer any questions. Its the desire to help beginners which drives us, replies like yours are a real turn off.

I wasn't asking anyone to solve my problem. I asked for suggestions. My problem had the breaks in them. By the way, I solved the problem without the your so called solutions. I think that if there is any disrespecting going on it is from you. It's okay to get off topic, but surely you all have a discussion board just for that and if not I would like to solve your problem, by saying you need one.

>By the way, I solved the problem without the your so called solutions.
I'm glad you solved your problem. Now I can put on my moderator hat and lock this thread to keep you from starting a real flame war.

>I think that if there is any disrespecting going on it is from you.
The first person to show disrespect was you when you threw away everyone's help to bitch and moan about how we were wasting your time. You clearly aren't interested in being nice, so don't expect us to be nice in return.

Can you give me some example problems from switch and nested if statement? plssss

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.