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

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.

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.

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?

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!

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>

#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

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.

> 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) ;
 }
Comments
ooh, good idea!
This article has been dead for over six months. Start a new discussion instead.