Jun 6, 2015 at 8:18pm UTC
I can't figure out how to get the double displayFunc(double paymentRate, double balanceWInterest) function to refresh to the new value for balanceWInterest, or balWInt in the main function. Can someone give me some insight on this?
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
// Cody Taylor
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
const string MONTH_PAY = "What will your monthly payment will be?" ;
const string OPTION_ONE = "1 - 10% of balance with interest" ;
const string OPTION_TWO = "2 - 20% of balance with interest" ;
const string OPTION_THREE = "3 - 30% of balance with interest" ;
const string ANSWER_INPUT1 = "Enter choice from menu above: " ;
const string BEG_BAL = "Enter your beginning balance: " ;
const string INTEREST = "Enter the credit card's annual interest rate: " ;
const double RATE1 = .10;
const double RATE2 = .20;
const double RATE3 = .30;
const int MIN_BALANCE = 100;
int monthPaymentFunc();
double beginningBal(string prompt);
double interestFunc(string promptTwo);
double displayFunc(double paymentRate, double balanceWInterest);
int main()
{
int userInput;
double bal;
double intRate;
double rateOfPay;
int numberMonths;
double balWInt;
double payment;
int month;
double monthsInt;
double endBal;
double beginBal;
cout << "This program calculates how long it will take to pay off your credit card balance." << endl << endl;
userInput = monthPaymentFunc() ;
bal = beginningBal(BEG_BAL) ;
intRate = interestFunc(INTEREST);
if (userInput == 1)
{
rateOfPay = RATE1;
}
else
if (userInput == 2)
{
rateOfPay = RATE2;
}
else
if (userInput == 3)
{
rateOfPay = RATE3;
}
balWInt = (bal * intRate) + bal;
payment = displayFunc( rateOfPay, balWInt);
month = 0;
cout << "RESULTS:" << endl;
cout << "-------------------------------------------------------------------" << endl;
cout << " Initial Month's Balance " << rateOfPay << "% End" << endl;
cout << "Month Balance Interest w/Interest Payment Balance" << endl;
cout << "-------------------------------------------------------------------" << endl;
month++;
monthsInt = bal * intRate;
endBal = balWInt - payment;
cout << month << setw(10) << bal << setw(10) << monthsInt << setw(10)
<< balWInt << setw(10) << payment << setw(10) << endBal << endl;
while (endBal > 0)
{
month++;
monthsInt = endBal * intRate;
bal = (endBal);
balWInt = (bal * intRate) + bal;
endBal = balWInt - payment;
cout << month << setw(10) << bal << setw(10) << monthsInt << setw(10)
<< balWInt << setw(10) << payment << setw(10) << endBal << endl;
}
cout << endl << endl;
system ("PAUSE" );
return 0;
}
int monthPaymentFunc()
{
int userChoice;
cout << MONTH_PAY << endl << setw(40) << OPTION_ONE << endl << setw(40)
<< OPTION_TWO << endl << setw(40) << OPTION_THREE << endl << ANSWER_INPUT1;
cin >> userChoice;
cout << endl;
while ((userChoice < 1) || (userChoice > 3))
{
cout << "Number must be one, two, or three. Try again. " ;
cin >> userChoice;
cout << endl;
}
return userChoice;
}
double beginningBal(string prompt)
{
double begBal;
cout << prompt;
cin >> begBal;
cout << endl;
while (begBal < MIN_BALANCE)
{
cout << "Beginning balance must be more than " << MIN_BALANCE << ", try again. " ;
cin >> begBal;
cout << endl;
}
return begBal;
}
double interestFunc(string promptTwo)
{
double interestRate;
double monthlyRate;
double divisionResult;
cout << promptTwo;
cin >> interestRate;
cout << endl;
while ((interestRate < 5) || (interestRate >= 26))
{
cout << "Error: Interest rate must be an integer between five, and twenty five." ;
cout << "Integer may be followed by a decimal." ;
cin >> interestRate;
cout << endl;
}
divisionResult = (interestRate / 100);
monthlyRate = (divisionResult / 12);
return monthlyRate;
}
double displayFunc(double paymentRate, double balanceWInterest)
{
double payment;
payment = (balanceWInterest * paymentRate);
return payment;
}
Last edited on Jun 6, 2015 at 8:19pm UTC
Jun 6, 2015 at 8:39pm UTC
What do you mean by "refresh"? Where/when?
Your displayFunc() performs a calculation. You call it on line 66, i.e. you compute a value and store it into variable "payment" on line 66. If you want to perform the same calculation for different input values later, then you have to call the function again.
Jun 6, 2015 at 8:56pm UTC
I need the payment value to refresh every time the while loop
while(endBal > 0)
{
month++;
monthsInt = endBal * intRate;
bal = (endBal);
balWInt = (bal * intRate) + bal;
endBal = balWInt - payment;
cout << month << setw(10) << bal << setw(10) << monthsInt << setw(10)
<< balWInt << setw(10) << payment << setw(10) << endBal << endl;
}
runs, so that the payment and the ending balance will both be accurate. Right now it just keeps the same payment value every time the loop runs. I intend on putting this ^^^ in a function once its running correctly, so I wont be able to call the displayFunc(double paymentRate, double balanceWInterest) function in the while loop. Is what I'm saying making any sense?
Jun 6, 2015 at 9:23pm UTC
Why do you think that you cannot call a function from a function?