Using a switch statement with nested if/else statements

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Jun 2007
Posts: 6
Reputation: lovetwins01 is an unknown quantity at this point 
Solved Threads: 0
lovetwins01 lovetwins01 is offline Offline
Newbie Poster

Using a switch statement with nested if/else statements

 
0
  #1
Jun 7th, 2007
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.
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,776
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Using a switch statement with nested if/else statements

 
0
  #2
Jun 7th, 2007
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'm here to prove you wrong.
Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Using a switch statement with nested if/else statements

 
0
  #3
Jun 7th, 2007
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...
-->sometimes i wanna take my toaster in a bath<--
Quick reply to this message  
Join Date: Jun 2007
Posts: 6
Reputation: lovetwins01 is an unknown quantity at this point 
Solved Threads: 0
lovetwins01 lovetwins01 is offline Offline
Newbie Poster

Re: Using a switch statement with nested if/else statements

 
0
  #4
Jun 8th, 2007
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.
Quick reply to this message  
Join Date: Jun 2007
Posts: 6
Reputation: lovetwins01 is an unknown quantity at this point 
Solved Threads: 0
lovetwins01 lovetwins01 is offline Offline
Newbie Poster

Re: Using a switch statement with nested if/else statements

 
0
  #5
Jun 8th, 2007
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.
Last edited by lovetwins01; Jun 8th, 2007 at 8:28 pm.
Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Using a switch statement with nested if/else statements

 
0
  #6
Jun 9th, 2007
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 ([code=c]), 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:
  1. case 'a':
  2. case 'A':...
you can do it this way:
  1. case ('a'||'A'):...
i'm sure narue will correct me if i'm wrong...
Last edited by Nichito; Jun 9th, 2007 at 1:14 am.
-->sometimes i wanna take my toaster in a bath<--
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,776
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Using a switch statement with nested if/else statements

 
0
  #7
Jun 9th, 2007
>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:
  1. case 1:
I'm here to prove you wrong.
Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Using a switch statement with nested if/else statements

 
0
  #8
Jun 9th, 2007
Originally Posted by lovetwins01 View Post
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

Originally Posted by Nichito View Post
i'm sure narue will correct me if i'm wrong...
You're wrong
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
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Using a switch statement with nested if/else statements

 
0
  #9
Jun 9th, 2007
but, it works with what lovetwins wants to do, since it will evaluate if package equals to 'a' or || 'A'...
-->sometimes i wanna take my toaster in a bath<--
Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Using a switch statement with nested if/else statements

 
0
  #10
Jun 9th, 2007
Originally Posted by WaltP
You're wrong
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
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...
Last edited by Nichito; Jun 9th, 2007 at 1:28 pm.
-->sometimes i wanna take my toaster in a bath<--
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC