Please help newbie

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 10
Reputation: shannonpaul is an unknown quantity at this point 
Solved Threads: 0
shannonpaul shannonpaul is offline Offline
Newbie Poster

Please help newbie

 
0
  #1
Aug 21st, 2007
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.

  1. include <iostream>
  2. #include <iomanip>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7. using std::setprecision;
  8. using std::ios;
  9. using std::setiosflags;
  10.  
  11. int main()
  12. {
  13. //declare constants and variables
  14. const double COMM_RATE1 = .02;
  15. const double COMM_RATE2 = .05;
  16. int sales = 0;
  17. double commission = 0.0;
  18.  
  19. //get input item
  20. cout << "Enter the sales amount: ";
  21. cin >> sales;
  22.  
  23. //calculate commission
  24. switch (sales)
  25. {
  26. case 100000.0 <= : cout << commission = sales * COMM_RATE1 << end1;
  27. break;
  28. case 100000.0 > : cout << commission = sales * COMM_RATE2 << end1;
  29. break;
  30.  
  31. default: cout << "error" << endl;
  32. //end switch
  33. }
  34. //display commission
  35. cout << setiosflags(ios::fixed) << setprecision(2);
  36. cout << "Commission: $" << commission << endl;
  37.  
  38. return 0;
  39. } //end of main function
Last edited by Ancient Dragon; Aug 21st, 2007 at 9:52 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Please help newbie

 
0
  #2
Aug 21st, 2007
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.
  1. //calculate commission
  2. if ( sales <= 100000.0 ) {
  3. commission = sales * COMM_RATE1;
  4. cout << commission << endl;
  5. } else {
  6. commission = sales * COMM_RATE2;
  7. cout << commission << endl;
  8. }
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.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: shannonpaul is an unknown quantity at this point 
Solved Threads: 0
shannonpaul shannonpaul is offline Offline
Newbie Poster

Re: Please help newbie

 
0
  #3
Aug 21st, 2007
Yes I know the if statement would be a better choice but I must you use the switch for an assignment. I am no slacker, and have fought this for 3 hrs. And finially decided to try for help.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Please help newbie

 
0
  #4
Aug 21st, 2007
but I must you use the switch for an assignment.
I don't think your teacher really wants you to have 100,000 separate cases, and that's exactly what you need to do what it looks like you're trying to do. To get it to work, this is what you have to do.
  1. switch ( sales ) {
  2. case 0:
  3. case 1:
  4. case 2:
  5. case 3:
  6. case 4:
  7. case 5:
  8. //...
  9. case 100000:
  10. commission = sales * COMM_RATE1;
  11. cout << commission << endl;
  12. break;
  13. default:
  14. commission = sales * COMM_RATE2;
  15. cout << commission << endl;
  16. break;
  17. }
You may not be a slacker, but that's still excessively tedious. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: shannonpaul is an unknown quantity at this point 
Solved Threads: 0
shannonpaul shannonpaul is offline Offline
Newbie Poster

Re: Please help newbie

 
0
  #5
Aug 21st, 2007
Thank you so so much. Now I have a much better understanding of where and why I was unable to get it right....thanks again!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: shannonpaul is an unknown quantity at this point 
Solved Threads: 0
shannonpaul shannonpaul is offline Offline
Newbie Poster

Re: Please help newbie

 
0
  #6
Aug 21st, 2007
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>
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7. using std::setprecision;
  8. using std::ios;
  9. using std::setiosflags;
  10.  
  11. int main()
  12. {
  13. //declare constants and variables
  14. const double COMM_RATE1 = .02;
  15. const double COMM_RATE2 = .05;
  16. double sales = 0.0;
  17. double commission = 0.0;
  18.  
  19. //get input item
  20. cout << "Enter the sales amount: ";
  21. cin >> sales;
  22.  
  23. //calculate commission
  24. if (sales <= 100000.0)
  25. commission = sales * COMM_RATE1;
  26. else
  27. commission = sales * COMM_RATE2;
  28. //end if
  29.  
  30. //display commission
  31. cout << setiosflags(ios::fixed) << setprecision(2);
  32. cout << "Commission: $" << commission << endl;
  33.  
  34. return 0;
  35. } //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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,616
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Please help newbie

 
0
  #7
Aug 21st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Please help newbie

 
1
  #8
Aug 22nd, 2007
> 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?
  1. enum { LESS_THAN_10000, NOT_LESS_THAN_10000 };
  2. int to_be_used_in_switch = sales < 100000.0 ?
  3. LESS_THAN_10000 : NOT_LESS_THAN_10000 ;
  4. switch( to_be_used_in_switch )
  5. {
  6. case LESS_THAN_10000:
  7. /* ... */ break ;
  8. case NOT_LESS_THAN_10000:
  9. /* ... */ break ;
  10. default:
  11. assert(false) ;
  12. }
Last edited by vijayan121; Aug 22nd, 2007 at 1:35 am.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC