While loop with nested if won't iterate

Can anyone tell me how to get this while loop to iterate. I know it has to do with the way it is reading the if statement, but I am not sure how to fix it. Instead of using the logic and saying "type is != to 't' so I will continue on with the loop", it acts as though it skips the line with the if statement and goes directly to the "break" line.


<code>
cout << "Lumber Calculator" << endl << endl << "Available type codes: ""P"" for pine , ""F"" for fir, ""C"" for cedar, ""M"" for maple, \n and ""O"" for oak. Press ""T"" for total." << endl
<< "Use type quantity width height length format. (i.e. P 10 2 4 8)" << endl << endl;
cout << "Enter first item: ";
cin >> type >> quantity >> width >> height >> length;

while (quantity != -1) {
boardFt = boardFeet (width, height, length);
typeWood = getType (type);
subTotal = (getPrice (type, boardFt))*quantity;
total += subTotal;
cout << quantity << " " << width << "x" << height << "x" << length << " " << typeWood << fixed << setprecision(2) << ", Costs: $" << subTotal << endl;
cout << "Enter item: ";
cin >> type;
if (type = 'T' || 't')
break;
else

cin >> quantity >> width >> height >> length;
}
cout << "Total cost: $" << total << endl;

system("pause");
return 0;
</code>
Idk maybe you should try == instead of = because == is for comparing and = is an assignment operator.
As vargoal says, you're using an assignment operator '=' instead of a comparison one '=='.
That part of the if condition is saying "assign type the value 'T' and check the result. The result will be 'T', which is non-zero and so will be interpreted as 'true'.

The 'or' part of the if condition just says 't', which is the value 't' and, as before, will evaluate to true (non-zero).

What you've written is "if (true or true)" with a side-effect of assigning type the value 'T'.

What you need to write is if (type == 'T' || type == 't').

Better still, you should place the constant value on the left-hand-side of the expression, then the compiler will catch these sorts of mistakes for you ...

if ('T' == type || 't' == type)

If you used an '=' by mistake there, the compiler would complain that you can't assign 'T' (a constant) the value of type.

You could also look into using toupper() and only checking against 'T' ...

if (toupper(type) == 'T')

Cheers,
Jim
Thanks guys! I guess I got tunnel vision. I am totally palm smacking my head right now! You rock!
Topic archived. No new replies allowed.