| | |
Using a switch statement with nested if/else statements
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2007
Posts: 6
Reputation:
Solved Threads: 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.
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.
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...
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<-- •
•
Join Date: Jun 2007
Posts: 6
Reputation:
Solved Threads: 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.
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.
#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.
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: you can do it this way: i'm sure narue will correct me if i'm wrong...
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:
C++ Syntax (Toggle Plain Text)
case 'a': case 'A':...
C++ Syntax (Toggle Plain Text)
case ('a'||'A'):...
Last edited by Nichito; Jun 9th, 2007 at 1:14 am.
-->sometimes i wanna take my toaster in a bath<-- >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 ('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:
C++ Syntax (Toggle Plain Text)
case 1:
I'm here to prove you wrong.
•
•
•
•
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.
Problem 1: You didn't use CODE tags as the background of the input box advises. Also here
Problem 2: Formatting code
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
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<-- ![]() |
Similar Threads
- Switch Statement (Java)
- Problems with switch statement (C++)
- Problems with switch statement (C++)
Other Threads in the C++ Forum
- Previous Thread: Quicksort problems
- Next Thread: i have a littel problem can any one help me
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





