C++ Help with if else if statement please!

I need to write a program calculating commission based off this info:
15% commission for the first $2,000.00 sales
20% commission for the next $1,000.00 sales
25% commission for the next $500.00 sales
30% commission for the next $500.00 sales
35% commission for the remaining sales

#include <iostream>

using namespace std;

int main()
{
double sales,commission;
cout<<"Enter Sales Amount: ";
cin>>sales;
if (sales<=2000.00)
commission=(.15*sales);
else if (sales<=3000.00)
commission=(.15*2000.00) + ((sales-2000.00)*.20);
else if (sales<=3500)
commission=(.15*2000.00) + (1000.00*.20) + ((sales-3000.00)*.25);
else if (sales<=4000)
commission=(.15*2000.00) + (1000.00*.20) + (500.00*.25) + ((sales-3500.00)*.30);
else (sales>4000.00);
commission=(.15*2000.00) + (1000.00*.20) + (500.00*.25) + (500*.30) + ((sales-4000.00)*.35);
cout<<endl;
cout<<"Sales Amount: "<<sales<<endl;
cout<<"Commission Earned: "<<commission<<endl;
return 0;
}

When i input small numbers i get a negative result and overall wrong numbers. What did i do wrong? Thank you!
More often than not when you get negative numbers it means your variable type ran out of room and looped back to the beginning which is usually a negative number.

try changing your double to a long double
long double still didnt fix it. do i have it written out correctly?
greatallan wrote:
long double still didnt fix it. do i have it written out correctly?

No you haven't.

1
2
    else (sales>4000.00);   //error on this line here - can you spot it 
    commission=(.15*2000.00) + (1000.00*.20) + (500.00*.25) + (500*.30) + ((sales-4000.00)*.35);

You are making this way too complicated.

all you really need is:

if(sales <=2000)
commission = .15 * sales

else if (sales <=3000)
commission = .20 * sales

else if (sales<=3500)
commission = .25 * sales

else if (sales<=4000)
commission = .30 * sales

else //anything over 4000
commission = .35 * sales
Topic archived. No new replies allowed.