You are calculating monthly payment before you enter the loop. noMonths is 12 at that point. When you loop through values 24, 36, etc., you never recalculate the monthly payment. Try moving line 120 to between lines 142 and 143.
Another thing to check. In line 42, try entering "0.0". Or better yet, change line 17 to
float price = 0.0;
. That should cause the program to break out of the loop prematurely. To fix this, change line 40 to
while (true)
Likewise, lines 58, 78 and 99 should be
while (! inRange)
or
while (true)
. Note that you break out of the loop right after you set inRange, so you don't actually end up using the inRange value anywhere.
One more subtle thing. Look at this code segment:
1 2 3 4 5 6 7 8 9 10 11 12
|
while (price)
{
cin >> price;
if (price > 50 && price < 50000) // Check that the input is greater than $50.00 and less than $50,000.00
{
cout << endl << endl;
break; // Leave while-loop
}
else
cout << endl << endl;
cout << " Input was invalid. Please put in a price between $50 and $50,000: $";
}
|
Your if clause is inside { }. Your else clause is actually just a single line because there are no curly braces after the else. The second line is general code, unrelated to the if/else construct. The indentation is actually wrong.
The code does the right thing only because you break out of the while loop when the if clause is executed. So only in "else" conditions will you execute the second line of code after the else.
You should
either add { } around both lines in the else clause
or Get rid of the
else
statement and outdent the 2 lines of code.