Hit a snag

In my C++ class, we are writing a program that takes some information from user input (cin) and calculates information for a customer at a used car dealership.
However, at the very end I hit a snag. This snag is found in the final customer report table. In this table, there is a section for a monthly payment options section and when it is displayed through the program, the number of months (noMonths) displays and increments correctly, but the monthly payment totals (monPayment) stays the same as the first option at 12 months. I have no idea how to fix it, and have scoured through the textbook to try to find an answer but to no avail. Please take a look and let me know. I think it has something to do with the fact that I placed it in an "if" statement inside of a "while" loop, but I'm not sure why that is the case.

Also, this is my first post here, so please let me know if I have done something incorrectly. Thanks!
Here is the code I used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>         // For cin and cout functions
#include <cmath>            // For exp function
#include <string>           // For string expressions in descriptions/directions
#include <iomanip>          // For setprecision function

using namespace std;

int main()
{
//************************************************************************************************************
// This section will declare and initialize all variables to be used throughout the program, along
// with brief summaries of the information they will hold.

// For ease of use, I will also include the name from the "CODE SECTIONS" table in a comment for each row in
// the table, such as "Main" for this section.

    float price;                // Price of the vehicle
    float downPayment;          // Down Payment
    float tradeIn;              // Trade in value for vehicle
    float loanAmt;              // Loan Amount (calculated)
    float annualIntRate;        // Annual Interest Rate (fraction)
    float annualIntPercent;     // Annual Interest Rate (percent)
    float monIntRate;           // Monthly Interest Rate (fraction)
    int noMonths = 12;               // Number of Monthly Payments (12, 24, 36, 48, & 60)
    float monPayment;           // Monthly Payment
    bool inRange = true;        // Used in loops to make sure that the user's input is within the specified range

//************************************************************************************************************
// This section will walk the user through the general purpose of this application and will walk
// them through inputting the data into the system for calculations.

    cout << "   This program is design to help you calculate the monthly car" << endl;
    cout << "   payment(s) for a single customer, or multiple different customers." << endl << endl;
    cout << "   To begin, you will be asked a series of questions to gather the information" << endl;
    cout << "   needed in the calculations." << endl << endl;

// Get price
    cout << "   Please put in the total price of the vehicle(between $50.00 and $50,000.00:  $";

    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:            $";
    }

// Get trade-in
    cout << "   Please put in the trade-in value (must be greater than or equal to $0.00" << endl;
    cout << "   and less than the cost of the vehicle):                                      $";

    inRange = false;                                // Resets the flag condition
    while (tradeIn)
    {
        cin >> tradeIn;
        if (tradeIn >= 0 && tradeIn < price)      // Check that the input is $0.00 or more and less than the price of the vehicle
        {
            inRange = true;                          // Set flag that event has been passed, and move on to next step
            cout << endl << endl;
            break;                                   // Leave while-loop
        }
        else
            cout << endl << endl;
            cout << "   Input was invalid. Please put in the trade-in value (must be greater than or equal" << endl;
            cout << "    to $0 and less than the cost of the vehicle)                                $";
    }

// Get down-payment
    cout << "   Please put in the down payment (must be greater than or equal to $0" << endl;
    cout << "   and less than the price minus the trade-in):                                 $";

    inRange = false;                                 // Resets the flag condition
    while (downPayment)
    {
        cin >> downPayment;
        if (downPayment >= 0 && downPayment < (price - tradeIn))
        {
            inRange = true;                          // Set flag that event has been passed, and move on to next step
            cout << endl << endl;
            break;                                   // Leave while-loop
        }
        else
            cout << endl << endl;
            cout << "   Input was invalid. Please put in the down payment (must be" << endl;
            cout << "   greater than or equal to $0.00 and less than the price minus" << endl;
            cout << "   the trade-in):                                                               $";
    }

// Get interest rate
    cout << "   Please put in the annual interest rate (in decimal-percent form" << endl;
    cout << "   (i.e. enter 0.086 for 8.6%)):                                                 ";

    inRange = false;                                 // Resets the flag condition
    while (annualIntRate)
    {
        cin >> annualIntRate;
        if (annualIntRate >= 0 && annualIntRate < 0.50)
        {
            inRange = true;                          // Set flag that event has been passed, and move on to next step
            cout << endl << endl << endl << endl;
            break;                                   // Leave while-loop
        }
        else
            cout << endl << endl;
            cout << "   Input was invalid. Please put in the annual interest rate" << endl;
            cout << "   (in decimal-percent form (i.e. enter 0.086 for 8.6%)):                        ";
    }

//************************************************************************************************************
// The section below will gather the information the user has put in and calculate the information requested.

    monIntRate = annualIntRate / 12.0;
    annualIntPercent = annualIntRate * 100.0;
    loanAmt = (price - downPayment - tradeIn);
    monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate,-noMonths));

//************************************************************************************************************
// This section will provide the format for the payment information chart.

cout << "   Honest Dave's Used Cars" << endl << endl;
cout << "   Vehicle price:          $" << fixed << setprecision(2) << price << endl;
cout << "   Trade-In Value:         $" << fixed << setprecision(2) << tradeIn << endl;
cout << "   Down Payment:           $" << fixed << setprecision(2) << downPayment << endl;
cout << "                -----------------" << endl;
cout << "   Loan Amount:            $" << fixed << setprecision(2) << loanAmt << endl << endl;

cout << "   Annual Interest Rate:   " << annualIntPercent << "%" << endl << endl;

cout << "   Monthly Payment Options:" << endl;
cout << "   ";

inRange = false;
while (noMonths)
{
    if (noMonths < 60)
    {
        noMonths = (noMonths + 12);         // To add the 12 months between options
        cout << noMonths << " Months              $" << fixed << setprecision(2) << monPayment << endl << "   ";
    }
    else
        inRange = true;
}

    return (0);
}


And the result:
Honest Dave's Used Cars

   Vehicle price:          $16900.00
   Trade-In Value:         $4000.00
   Down Payment:           $2000.00
                -----------------
   Loan Amount:            $10900.00

   Annual Interest Rate:   7.50%

   Monthly Payment Options:
   24 Months              $945.65
   36 Months              $945.65
   48 Months              $945.65
   60 Months              $945.65

Hi,

It's nothing to do with the syntax, it's a logical error.

Have a look at where monPayment is calculated, and work from there.

Best to use a debugger, hopefully there is GUI one in your IDE. Set a watch list of variables, step through the code 1 line at a time, see how the values change, deduce where it goes wrong.
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.
Thanks for the help! Got it figured out! Works like a charm now!
Topic archived. No new replies allowed.