Minor Problem

It only runs the first else if statement no matter what i input.. why??
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

int numShirt;
double priceShirt = 12;
double discount;
double discountPrice;
double totalCost;

cout << "how many shirts would you like?";
cin >> numShirt;

if (numShirt < 5, discount = .00);
else if (numShirt >= 5, discount =.10);
else if (numShirt >=11, discount =.15);
else if (numShirt >=21, discount =.20);
else if (numShirt >=31, discount =.25);

discount = priceShirt * discount;
discountPrice = priceShirt - discount;
totalCost = discountPrice * numShirt;

cout << setprecision(2) << fixed;
cout << "The cost per shirt is $" << discountPrice << " and the total cost is $ " << totalCost;

system("pause");

return 0;
}
Try this subtle change:

1
2
3
4
5
if (numShirt < 5) discount = .00;
else if	(numShirt >= 5) discount =.10;
else if (numShirt >=11) discount =.15;
else if (numShirt >=21) discount =.20;
else if (numShirt >=31) discount =.25;
The second part of your if statement:

if (numShirt < 5, discount = .00);
I'm pretty sure needs to placed:

1
2
3
4
if (numShirt < 5)
{
    discount = 0.00;
}


You might also benefit from looking at this tutorial on if statements:

http://www.cplusplus.com/doc/tutorial/control/

See the part on Conditional structure: if and else.

P.S.: Use code tags! So your code will be more readable [code]your code[/code]. So your code will look like this:
yourcode
Last edited on
I'm not quite sure what you think the comma does, but I assure it does not do what you think it does. For expression1, expression2 it evaluates expression1, then throws it out, then uses expression2. So if (numShirt < 5, discount = .00); evaluates numShirt < 5 and whether or not it is true or false, it ignores it and throws it out. Then it evaluates discount = .00, which returns 0 which is false. Then it does the same until it gets to discount =.10 which returns 0.1, which is nonzero, so it is true, which then ends the else if sequence. Also, a ; should not come immediatly after an if statement (except for very specific circumstances)

Replace each line with the equivalant of if (numShirt < 5) discount = .00;
Last edited on
thank you very much, i was very confused on my comma usage... that did help but now it will only output correctly for if (numShirt < 5) and else if (numShirt <= 5)
Topic archived. No new replies allowed.