943,589 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 822
  • C++ RSS
Aug 21st, 2007
0

Please help newbie

Expand Post »
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)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shannonpaul is offline Offline
10 posts
since Aug 2007
Aug 21st, 2007
0

Re: Please help newbie

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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 21st, 2007
0

Re: Please help newbie

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shannonpaul is offline Offline
10 posts
since Aug 2007
Aug 21st, 2007
0

Re: Please help newbie

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 21st, 2007
0

Re: Please help newbie

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shannonpaul is offline Offline
10 posts
since Aug 2007
Aug 21st, 2007
0

Re: Please help newbie

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>
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shannonpaul is offline Offline
10 posts
since Aug 2007
Aug 21st, 2007
0

Re: Please help newbie

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Aug 22nd, 2007
1

Re: Please help newbie

> 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?
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: gmp little help please
Next Thread in C++ Forum Timeline: private constructors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC