| | |
Please help newbie
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 10
Reputation:
Solved Threads: 0
I am not sure why I am getting syntax errors in lines 29 and 31. When I run the console my return values are correct, but have syntax errors.
C++ Syntax (Toggle Plain Text)
include <iostream> #include <iomanip> using std::cout; using std::cin; using std::endl; using std::setprecision; using std::ios; using std::setiosflags; int main() { //declare constants and variables const double COMM_RATE1 = .02; const double COMM_RATE2 = .05; int sales = 0; double commission = 0.0; //get input item cout << "Enter the sales amount: "; cin >> sales; //calculate commission switch (sales) { case 100000.0 <= : cout << commission = sales * COMM_RATE1 << end1; break; case 100000.0 > : cout << commission = sales * COMM_RATE2 << end1; break; default: cout << "error" << endl; //end switch } //display commission cout << setiosflags(ios::fixed) << setprecision(2); cout << "Commission: $" << commission << endl; return 0; } //end of main function
Last edited by Ancient Dragon; Aug 21st, 2007 at 9:52 pm. Reason: add code tags
I think you're trying to use a switch to handle a range of values. That's not possible without one case for each value in the range. You should be using an if statement instead.
I fixed two other bugs. I don't think an assignment operation is legal in a cout, and it's endl with a lower case L, not end1 with the number 1.
C++ Syntax (Toggle Plain Text)
//calculate commission if ( sales <= 100000.0 ) { commission = sales * COMM_RATE1; cout << commission << endl; } else { commission = sales * COMM_RATE2; cout << commission << endl; }
The truth does not change according to our ability to stomach it.
•
•
•
•
but I must you use the switch for an assignment.
To get it to work, this is what you have to do. C++ Syntax (Toggle Plain Text)
switch ( sales ) { case 0: case 1: case 2: case 3: case 4: case 5: //... case 100000: commission = sales * COMM_RATE1; cout << commission << endl; break; default: commission = sales * COMM_RATE2; cout << commission << endl; break; }
You might be confused about what the switch needs to be used for. Can you paste the requirements for your program so I can see what needs to be done? The truth does not change according to our ability to stomach it.
•
•
Join Date: Aug 2007
Posts: 10
Reputation:
Solved Threads: 0
What I have to do is replace the following code with a switch. One problem I am having is switching a double....so I madw it an int, which could not be used with dollar figure.
/Ch6AppE09.cpp - calculates and displays a commission
//Created/revised by <your name> on <current date>
/Ch6AppE09.cpp - calculates and displays a commission
//Created/revised by <your name> on <current date>
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using std::cout; using std::cin; using std::endl; using std::setprecision; using std::ios; using std::setiosflags; int main() { //declare constants and variables const double COMM_RATE1 = .02; const double COMM_RATE2 = .05; double sales = 0.0; double commission = 0.0; //get input item cout << "Enter the sales amount: "; cin >> sales; //calculate commission if (sales <= 100000.0) commission = sales * COMM_RATE1; else commission = sales * COMM_RATE2; //end if //display commission cout << setiosflags(ios::fixed) << setprecision(2); cout << "Commission: $" << commission << endl; return 0; } //end of main function
Last edited by Ancient Dragon; Aug 21st, 2007 at 9:52 pm. Reason: add code tags -- please use them in future posts
Not a good idea to use doubles in switch statements because they are often inprecice due to the way they are stored in memory. If you really have to use switch instead of that if statement then you should multiply it by 100 then assigned the result to an integer. Be careful that you don't get data overflow errors doing that.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> I know the if statement would be a better choice but I must you use the switch for an assignment
would this make your teacher happy?
would this make your teacher happy?
C++ Syntax (Toggle Plain Text)
enum { LESS_THAN_10000, NOT_LESS_THAN_10000 }; int to_be_used_in_switch = sales < 100000.0 ? LESS_THAN_10000 : NOT_LESS_THAN_10000 ; switch( to_be_used_in_switch ) { case LESS_THAN_10000: /* ... */ break ; case NOT_LESS_THAN_10000: /* ... */ break ; default: assert(false) ; }
Last edited by vijayan121; Aug 22nd, 2007 at 1:35 am.
![]() |
Similar Threads
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- Questions about building a system (was: newbie) (Troubleshooting Dead Machines)
- Best free C/C++ compiler for a newbie? (C++)
- help newbie alert needs help with login page (ASP.NET)
- newbie needs help, basic mfc stuff (C++)
- Hello, newbie here... (Geeks' Lounge)
- Book For Newbie (C++)
- Newbie - how do I start C++ programming? (C++)
- PHP newbie, project feasibility (PHP)
- How to network two Win98 machines (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: gmp little help please
- Next Thread: private constructors
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






