Hello again...

I am writing a program that will act as a loan calculator. I do need a bit of help though. The main thing is that I have to program set up with the features I want, but errors keep giving me trouble. I have been putting out fires with warnings and errors for hours now and every time I fix one, another pops up. Here is the code:
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
#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;

// Function prototypes
float getPrice (float);
float getTradeIn (float, float);
float getDownPayment (float, float, float);
float getInterestRate (float);
float calcMonPayment ();
float displayLoanSchedule();

int main ()
{
    float price;
    float tradeIn;
    float downPayment;
    float annualIntRate;

    //************************************************************************************************************
    // 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;

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

    cout << "Please put in the trade in value (greater than or equal to 0, but less than price of car";
    tradeIn = getTradeIn(tradeIn, price);
    cout << tradeIn;

    cout << "Please put in the down payment value (greater than or equal to 0, but less than" << endl;
    cout << "the price minus the trade in value)";
    downPayment = getDownPayment(downPayment, price, tradeIn);
    cout << downPayment;

    cout << "Please put in the annual interest rate as a decimal (8% = 0.08). This can't be greater" << endl;
    cout << "than 19% (or 0.19)";
    annualIntRate = getInterestRate(annualIntRate);
    cout << annualIntRate;

}

/****************************************************************************
*   This section will list all functions to be used throughout the program. *
****************************************************************************/

float getPrice (float price)
{
    do
    {
        cin >> price;
    }
    while (price > 75 && price >= 48500);

    return getPrice(price);
}

float getTradeIn (float tradeIn, float price)
{
    do
    {
        cin >> tradeIn;
    }
    while (tradeIn >= 0 && tradeIn < price);

    return getTradeIn(tradeIn, price);
}

float getDownPayment (float downPayment, float price, float tradeIn)
{
    do
    {
        cin >> downPayment;
    }
    while (downPayment >= 0 && downPayment < ((price) - (tradeIn)));

    return getDownPayment(downPayment, price, tradeIn);
}

float getInterestRate (float annualIntRate)
{
    do
    {
        cin >> annualIntRate;
    }
    while (annualIntRate > 0 && annualIntRate <= 0.19);

    return getInterestRate(annualIntRate);
}

float calcMonPayment(float annualIntRate, float price, float downPayment, float tradeIn, float monPayment[5], float monIntRate, float annualIntPercent, float loanAmt)
{
    monIntRate = annualIntRate / 12.0;
    annualIntPercent = annualIntRate * 100.0;
    loanAmt = price - downPayment - tradeIn;

    monPayment[0] = (loanAmt * monIntRate)/pow((1.0-(1+monIntRate)), -12);
    monPayment[1] = (loanAmt * monIntRate)/pow((1.0-(1+monIntRate)), -24);
    monPayment[2] = (loanAmt * monIntRate)/pow((1.0-(1+monIntRate)), -36);
    monPayment[3] = (loanAmt * monIntRate)/pow((1.0-(1+monIntRate)), -48);
    monPayment[4] = (loanAmt * monIntRate)/pow((1.0-(1+monIntRate)), -60);
}

float displayLoanSchedule (float price, float tradeIn, float downPayment, float loanAmt, float annualIntRate, float annualIntPercent, float monIntRate, int noMonths, float monPayment[5])
{
    //************************************************************************************************************
// 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 Payments:" << endl;
cout << "12 Months   " << monPayment[0] << endl;
cout << "24 Months   " << monPayment[1] << endl;
cout << "36 Months   " << monPayment[2] << endl;
cout << "48 Months   " << monPayment[3] << endl;
cout << "60 Months   " << monPayment[4] << endl;
}


And these are the current errors:

||=== Build: Debug in LoanCalculator_Final (compiler: GNU GCC Compiler) ===|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp||In function 'float calcMonPayment(float, float, float, float, float*, float, float, float)':|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp|118|warning: no return statement in function returning non-void [-Wreturn-type]|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp||In function 'float displayLoanSchedule(float, float, float, float, float, float, float, int, float*)':|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp|140|warning: no return statement in function returning non-void [-Wreturn-type]|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp||In function 'int main()':|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp|40|warning: 'price' is used uninitialized in this function [-Wuninitialized]|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp|44|warning: 'tradeIn' is used uninitialized in this function [-Wuninitialized]|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp|49|warning: 'downPayment' is used uninitialized in this function [-Wuninitialized]|
D:\C++_Program_Assignments\Ch. 11\LoanCalculator_Final\main.cpp|54|warning: 'annualIntRate' is used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 error(s), 6 warning(s) (0 minute(s), 0 second(s)) ===|
||=== Run: Debug in LoanCalculator_Final (compiler: GNU GCC Compiler) ===|



(As a side note, there are 8 lines I left off at the beginning simply for security purposes. Calculate those in if you want accurate line count in the output)
Last edited on
> I have been putting out fires with warnings and errors for hours now
> and every time I fix one, another pops up.
Well you should have started with something simple.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
float getPrice ();

int main ()
{
    float price;
    cout << "Please put in the total price of the vehicle(between $50.00 and $50,000.00:  $";
    price = getPrice();
    cout << price;
}

//!! This doesn't need parameters
float getPrice ()
{
    float price;
    do
    {
        cin >> price;
    }
    while (price > 75 && price >= 48500);

    //!! no, don't call yourself recursively!
    //!! return getPrice(price);
    return price;
}


The objective isn't to write 100's of lines of code all at once, only to be smacked in the mouth with a load of errors you don't know how to fix.

One new function, code block (ie matched { }) or loop at once, which you then test to see if it compiles and runs. With experience, you'll be able to write more code between compile/test cycles, but don't step out of your comfort zone too far.

For example, that would have prevented the systemic errors of passing parameters to functions which don't need parameters, and calling the function recursively in an attempt to return a result.

Start with placeholder functions like
1
2
3
4
float getDownPayment ()
{
    return 1200.0;
}

It gets you something to compile and test, and you can then stepwise refine it to.
1
2
3
4
5
6
float getDownPayment ()
{
    float downpayment;
    cin >> downpayment;
    return downpayment;
}

And then you can add your validation or whatever.

At risk of repeating Salem C's advice, as a beginner, you should be hitting the compile button and fixing any errors or warnings roughly every time you enter a line of code.

As a beginner, writing a program of a hundred or so lines, I'd expect you to have hit the compile button hundreds and hundreds of time before you have a complete, working program.
Last edited on
Topic archived. No new replies allowed.