The output of my code in C++ keeps comiing out as 0 instead of the total of accu
ID: 3665631 • Letter: T
Question
The output of my code in C++ keeps comiing out as 0 instead of the total of accumulated interest. Can someone show me whats worng in my code?
Here is the question:
Write a function declaration for a function that computes interest on a credit card account balace. The function takes arguments for the intitial balance, the monthly intrest rate, and the number of months for which interest must be paid. The vakue returned is the interest due. Do not forget to compound the interst-that is, to charge inteest on the interest due. The interest due is added to the balance due, and the interest and interest for the next month is computed using this larger balance. Use a while loop that is similar to (but need not be identical to) the one shown in Display 2.14:
A Loop Body with Several Statements:
do
{
Statement_1
Statement_2
…
Statement_Last
} while (Boolean _Expression);
A Loop Body with a Single Statement:
do
Statement
while (Boolean_Expression);
Embed the function in a progrsm that reads the value for the interest rate, initial account balance, and number of months, then out puts the interest due. Embed your function definition in a program that lets the user compute interest due on a credit account balance. the program should allow the user to repeat the calculation until the user says he or she wants to end the program.
Here is my code:
#include<iostream>
using namespace std;
// Function definition
double interest (double , double , int );
int main()
{
// Decleration of veriables.
double Balance, interestRate, total;
int months;
char choice;
do
{
// Inputting data that needs to be calculated.
cout<<"Credit card interest."<<endl;
cout<<"Enter doubles: initial balance, monthly interest rate as"<<endl;
cout<<"a decimal faction, e.g. for 1.5% per month write 0.015"<<endl;
cout<<"and int months the bill has run."<<endl;
cout<<"I will give you the interest that has accumulated."<<endl;
cin>>Balance;
cin>>interestRate;
cin>>months;
// Call the function.
total=interest(Balance, interestRate, months);
// Outputting interest.
cout<<"Interest accumulated = : "<<total<<endl;
// Input chice to repeat.
cout<<"Y or y repeats, any other character quits.";
cin>>choice;
}
while(choice == 'y' || choice == 'Y');
// Pause system for a while
// system("pause");
}
// Function definition
double interest (double intitialBalance, double interestRate, int months)
{
double initialBalance;
double total = 0;
int i;
for (i = months; i>=0; i--)
{
total += ((initialBalance * interestRate)/(100 *12));
initialBalance += ((initialBalance *(100+interestRate))/(100 *12));
}
return total;
}
The output should look like this:
Credit card interest
Enter doubles: initial balance, monthly interest rate as
a decimal fraction, e.g. for 1.5% per month write 0.015
and int months the bill fas run.
I will give you the interest that has accumulated.
1000
0.015
121
Interest accumulated = $195.62 (here is my problem my output reads 0)
Y or y repeats, any other character quits
Explanation / Answer
Change the interst calculation formula as :
cmpndIntr = initialBalance * pow( (1.0 + interestRate), months);
The entire changed code:
#include <iostream.h> // input output stream header file
#include <stdio.h> // standard input output header file
#include <stdlib.h> // standard library header file
//#include <string.h> // header file with string function
// #include <conio.h> // console input output header file
#include <math.h>
//using namespace std;
// Function definition
double interest (double , double , int );
int main()
{
// Decleration of veriables.
double Balance, interestRate, total;
int months;
char choice;
do
{
// Inputting data that needs to be calculated.
cout<<"Credit card interest."<<endl;
cout<<"Enter doubles: initial balance, monthly interest rate as"<<endl;
cout<<"a decimal faction, e.g. for 1.5% per month write 0.015"<<endl;
cout<<"and int months the bill has run."<<endl;
cout<<"I will give you the interest that has accumulated."<<endl;
cin>>Balance;
cin>>interestRate;
cin>>months;
// Call the function.
total=interest(Balance, interestRate, months);
// Outputting interest.
cout<<"Interest accumulated = : "<<total<<endl;
// Input chice to repeat.
cout<<"Y or y repeats, any other character quits.";
cin>>choice;
}
while(choice == 'y' || choice == 'Y');
// Pause system for a while
// system("pause");
}
// Function definition
double interest (double intitialBalance, double interestRate, int months)
{
double initialBalance;
double total = 0;
double cmpndIntr = 0.0;
int i;
for (i = months; i>=0; i--)
{
//initialBalance += ((initialBalance *(100+interestRate))/(100 *12));
//total += ((initialBalance * interestRate)/(100 *12));
cmpndIntr = initialBalance * pow( (1.0 + interestRate), months);
}
return cmpndIntr;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.