954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please help newbie

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.

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
shannonpaul
Newbie Poster
10 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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.

//calculate commission
if ( sales <= 100000.0 ) {
  commission = sales * COMM_RATE1;
  cout << commission << endl;
} else {
  commission = sales * COMM_RATE2;
  cout << commission << endl;
}

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.

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

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.

shannonpaul
Newbie Poster
10 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 
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.

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 may not be a slacker, but that's still excessively tedious. :D 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?

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

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!

shannonpaul
Newbie Poster
10 posts since Aug 2007
Reputation Points: 10
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 on #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

shannonpaul
Newbie Poster
10 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

> 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?

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) ;
 }
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You