C Programming When I try to run the program below it will on
C Programming
When I try to run the program below, it will only ask the printf statements. When it gets to the last one, I input a value and press enter, but the program won\'t run. It just moves down a line in terminal and still allows more input but never runs. I think it hows to do with how I\'m calling the functions but Im not sure how to better call them. Thanks in advance! Here is the homework prompt: For this program you will be determining if it is better to put as much money as you can towards your student loans before saving for retirement or if it is better to only pay the minimum payment on your loan and invest the rest.
#include <stdio.h>
#include <stdlib.h>
double minLoans(double monthlyPay, double owed, double rate, double minPay, double returnRate, int age, int retirementAge);
double maxLoans(double monthlyPay, double owed, double rate, double minPay, double returnRate, int age, int retirementAge);
void investments(double monthlyPay, double owed, double rate, double minPay, double returnRate, int age, int retirementAge);
double minLoans(double monthlyPay, double owed, double rate, double minPay, double returnRate, int age, int retirementAge) {
//calculates savings earned if the minimum is put towards the loans and the rest invested
int numMonths;
int i = 0;
double owed2 = 0.0;
double loansLeft = 0.0;
double savings = 0.0;
numMonths = (retirementAge - age) * 12; //number of months between current age and retirement age
for (i = 0; owed >= 0; ++i) {
savings += (savings * (returnRate / 12.0)); //return interest per month applied to savings
savings += (monthlyPay - minPay);
if (minPay > owed) { //if the minimum payment is greater than the amount owed
savings += (minPay - owed); //the remaining amount is added to savings
owed = 0; //the loan is payed off
}
else {
owed += (owed * (rate / 12.0)); //amount owed after interest
owed -= minPay; //amount owed after minimum payment
}
}
if (i > numMonths) { //if loan takes longer to pay off then time allotted, this statement calculates the amount of loans left to pay
owed2 = owed;
for (i = 0; i < numMonths; ++i) {
owed2 += (owed2 * (rate / 12.0)); //interest added to total owed
owed2 -= minPay; //monthly minimum payment subtracted from total owed
}
loansLeft = owed - owed2;
printf(\"Warning! After you retire you will still have $%.2lf in loans left.\ \", loansLeft);
}
return savings;
}
double maxLoans(double monthlyPay, double owed, double rate, double minPay, double returnRate, int age, int retirementAge) {
int i = 0;
int numMonths, monthsNeeded;
double savings2 = 0.0;
double owed2 = 0.0;
double loansLeft = 0.0;
numMonths = (retirementAge - age) * 12; //number of months between current age and retirement age
for (i = 0; owed >= 0; ++i) { //runs until loan is payed off
owed += (owed * (returnRate / 12.0));
owed -= monthlyPay;
}
monthsNeeded = i;
if (i > numMonths) { //if loan takes longer to pay off then time allotted, this statement calculates the amount of loans left to pay
owed2 = owed;
for (i = 0; i < numMonths; ++i) {
owed2 += (owed2 * (rate / 12.0)) + owed2;
owed2 -= monthlyPay;
}
loansLeft = owed - owed2;
printf(\"Warning! After you retire you will still have $%.2lf in loans left.\ \", loansLeft);
savings2 = 0;
}
else {
for (i = 0; i < (numMonths - monthsNeeded); ++i) {
savings2 += (savings2 * (returnRate / 12.0));
savings2 += monthlyPay;
}
}
return savings2;
}
void investments(double monthlyPay, double owed, double rate, double minPay, double returnRate, int age, int retirementAge) {
//checks which option results in higher savings amount
double savings = 0.0;
double savings2 = 0.0;
minLoans(monthlyPay, owed, rate, minPay, returnRate, age, retirementAge);
maxLoans(monthlyPay, owed, rate, minPay, returnRate, age, retirementAge);
if (savings > savings2) { //paying minimum on loan is the better option
printf(\"\ You should only make the minimum payments on your loan and apply the rest towards retirement.\ \");
printf(\"If you do you will have $%.2lf when you retire as opposed to $%.2lf if you payed off your loan before investing.\ \", savings, savings2);
}
else if (savings2 > savings) { //paying off loans first is the better option
printf(\"You should apply all $%.2lf towards your loan before making any investments.\ \", monthlyPay);
printf(\"If you do you will have $%.2lf when you retire as opposed to $%.2lf if you only made minimum payments.\ \", savings2, savings);
}
}
int main () {
double monthlyPay, owed, rate, minPay, returnRate = 0.0;
int age, retirementAge;
printf(\"Enter how much money you will be putting towards loans/retirement each month: \");
scanf(\"%lf\", &monthlyPay);
printf(\"Enter how much you owe in loans: \");
scanf(\"%lf\", &owed);
printf(\"Enter the annual interest rate of the loans: \");
scanf(\"%lf\", &rate);
printf(\"Enter your minimum monthly loan payment: \");
scanf(\"%lf\", &minPay);
printf(\"Enter your current age: \");
scanf(\"%d\", &age);
printf(\"Enter the age you plan to retire at: \");
scanf(\"%d\", &retirementAge);
printf(\"Enter the annual rate of return you predict for your investments: \");
scanf(\" %lf\", &returnRate);
investments(monthlyPay, owed, rate, minPay, returnRate, age, retirementAge);
return 0;
}
Solution
In investments function , after we have called minLoans and maxLoans functions.
Since those 2 functions are returning the savings (double type) , so we have to store it in savings and savings2 variables respectively. Because you have declared both savings and savings2 as 0.0 (of double type) and when you are checking which one is greater in the if-else statement following the declaration statement, it is going to neither \"if\" nor to \"else\" since both are equal (0.0) as per your logic.
Please find below the modified code for investments function :
void investments(double monthlyPay, double owed, double rate, double minPay, double returnRate, int age, int retirementAge) {
//checks which option results in higher savings amount
double savings = 0.0;
double savings2 = 0.0;
savings=minLoans(monthlyPay, owed, rate, minPay, returnRate, age, retirementAge);
savings2=maxLoans(monthlyPay, owed, rate, minPay, returnRate, age, retirementAge);
if (savings > savings2) { //paying minimum on loan is the better option
printf(\"\ You should only make the minimum payments on your loan and apply the rest towards retirement.\ \");
printf(\"If you do you will have $%.2lf when you retire as opposed to $%.2lf if you payed off your loan before investing.\ \", savings, savings2);
}
else if (savings2 > savings) { //paying off loans first is the better option
printf(\"You should apply all $%.2lf towards your loan before making any investments.\ \", monthlyPay);
printf(\"If you do you will have $%.2lf when you retire as opposed to $%.2lf if you only made minimum payments.\ \", savings2, savings);
}
}


