writing a program for loans

For context I have to Write a program that will take three inputs: the amount the consumer needs to receive, the interest rate, and the duration of the loan in months with a loop asking if you want to do it again. Here's my coding and have been trying with void functions, but so far it has been fruitless. Almost everything works except the loop where it is asking if you want to do it again. Any pointers will help greatly.


#include<iostream>
#include<cmath>

using namespace std;

void reader_input(float & loan, float & rate, float & time,char & awnser);
void calculations(float & rate, float & loan, float & time, float & intrest, float & totalintrest, float & debt, float & monthlypay, float & years,char & awnser);
void read_output(float & debt, float & monthlypay, float & totalintrest, char & anwser, float & loan, float & rate, float & time, float & intrest, float & years);

int main()
{
float loan, rate, time, intrest, totalintrest, debt, monthlypay, years;
char anwser;
reader_input(loan, rate, time,anwser);
calculations(rate, loan, time, intrest, totalintrest, debt, monthlypay, years,anwser);
read_output(debt, monthlypay, years, anwser,loan,rate,time,intrest,years);
return 0;
}


void reader_input(float & loan, float & rate, float & time,char & anwser)
{

cout << "Enter in the loan you want in dollars." << endl;
cin >> loan;
cout << "What will the intrest rate be percentage wise? (value must be 1 to 25)" << endl;
cin >> rate;
cout << "Enter in the duration of the loan in months." << endl;
cin >> time;

}

void calculations(float & rate, float & loan, float & time, float & intrest, float & totalintrest, float & debt, float & monthlypay, float & years,char & anwser)
{
rate = rate / 100;
intrest = loan * rate;
years = time / 12;
totalintrest = intrest * years;
debt = totalintrest + loan;
monthlypay = debt / time;
}

void read_output(float & debt, float & monthlypay, float & totalintrest, char & anwser,float & loan,float & rate,float & time,float & intrest, float & years)
{
do {
cout << "The debt is " << debt << " dollars." << endl;
cout << "The monthly pay is " << monthlypay << " dollars." << endl;
cout << "would you like to do this again? Press Y for yes or N for no." << endl;
cin >> anwser;
return reader_input(loan, rate, time, anwser);
return calculations(rate, loan, time, intrest, totalintrest, debt, monthlypay, years, anwser);
} while (anwser == 'Y');


}

The last two lines in the do while loop are interfering as it ignores the "while" part and continues forever, but I can't fix both the loop and send the code back up if the user says yes to do it again.
Last edited on
How about doing it more like this: (it's not supposed to be perfect, just giving you ideas):

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
#include<iostream>

using namespace std;

void reader_input(float &loan, float &rate, int &time) {
    cout << "Enter in the loan you want in dollars: ";
    cin >> loan;
    cout << "What will the interest rate be percentage wise? (value must be 1 to 25): ";
    cin >> rate;
    cout << "Enter in the duration of the loan in months: ";
    cin >> time;
}

void calculations(float &rate, float &loan, int &time, float &interest, float &totalinterest,
                    float &debt, float &monthlypay, float &years) {
    rate = rate / 100;
    interest = loan * rate;
    years = time / 12;
    totalinterest = interest * years;
    debt = totalinterest + loan;
    monthlypay = debt / time;
}

void read_output(float &debt, float &monthlypay) {
    cout << "The debt is " << debt << " dollars." << endl;
    cout << "The monthly payment is " << monthlypay << " dollars." << endl;
    cout << "Would you like to do this again? Press Y for yes or N for no." << endl;
}

int main(){
    char answer;
    do {
        float loan, rate, interest, totalinterest, debt, monthlypay, years;
        int time;  // I'm guessing you only intend to allow whole numbers (1-12)

        reader_input(loan, rate, time);
        calculations(rate, loan, time, interest, totalinterest, debt, monthlypay, years);
        read_output(debt, monthlypay);
        cin >> answer;
    } while (toupper(answer) == 'Y');

return 0;
}

Enter in the loan you want in dollars: 100
What will the interest rate be percentage wise? (value must be 1 to 25): 6
Enter in the duration of the loan in months: 12
The debt is 106 dollars.
The monthly payment is 8.83333 dollars.
Would you like to do this again? Press Y for yes or N for no.
y
Enter in the loan you want in dollars: 

Good luck.
Last edited on
Thank you very much! It now seems to work just fine now. I guess I pretty much needed to move the loop into the int main. Hope you have a good night.
Topic archived. No new replies allowed.