My problem with this program is within my do while loop. From line 33 to line 47. Please find attached the original intention in the pdf document
`

``````//Program to show loan and interest calculation
#include <iostream>      // for cin, cout
#include <string>

using namespace std;
const double ANN_INTEREST=0.05;         //simple annual interest rate of 5%

int main()
{
double loan, ann_interest, monthly_payment, interest_rate, interest_paid, rem_bal, total_interest_paid, payment, month_interest=(1/12);     //declaration of loan and interest rate
int payment_count=1;                //declaration of the payment serial numbers

// Welcome message
cout<< "\$\$-\$+\$-\$+\$-\$+\$-\$+\$-\$+\$-\$\$\n"
<< "\$\$                     \$\$\n"
<< "... Topiloe's Bank ... \$\$\n"
<< "\$\$                     \$\$\n"
<< "\$\$-\$+\$-\$+\$-\$+\$-\$+\$-\$+\$-\$\$\n";

cout<<"What is the amount of your loan? ";
cin>>loan;
cout<<"What is the interest rate? ";
cin>>interest_rate;
cout<<endl;
monthly_payment=loan*ANN_INTEREST;
cout<<"Monthly payment on a loan of "<<loan<<" is "<<monthly_payment<<endl;
cout<<endl;
cout<<"Payment Details: \n";
cout<<"Payment Number\t"<<"Interest Paid\t"<<"Remaining Balance\n";
cout<<"--------------\t"<<"-------------\t"<<"-----------------\n";
payment=(20/100)*loan;
interest_paid=(interest_rate/100)*loan*month_interest;
rem_bal=loan-interest_paid;
cout<<payment_count<<"\t\t"<<interest_paid<<"\t\t"<<rem_bal<<endl;

do
{
interest_paid=((interest_rate/100)*rem_bal)*month_interest;
rem_bal=payment-interest_paid;
payment_count++;
cout<<payment_count<<"\t\t"<<interest_paid<<"\t\t"<<rem_bal<<endl;
payment_count++;
total_interest_paid=+interest_paid;
}
while (rem_bal!=0);
cout<<"Total interest paid is "<<total_interest_paid<<endl;
``````

`

LINE 33 is doing integer division which means there are no decimals. So 20/100 is 0. You need to use float, for example`20.0/100.0`

``````while (rem_bal!=0);
``````

... is a problem, since ...

your running programm may never stop ...

since, the double value rem_bal may never be exactly zero.

When working with money, long long int ( or sufficently big integer value holding objects) ... is the way to go.

you could code it: `while (rem_bal > 0.0)`

## All 10 Replies

LINE 33 is doing integer division which means there are no decimals. So 20/100 is 0. You need to use float, for example`20.0/100.0`

``````while (rem_bal!=0);
``````

... is a problem, since ...

your running programm may never stop ...

since, the double value rem_bal may never be exactly zero.

When working with money, long long int ( or sufficently big integer value holding objects) ... is the way to go.

You want your values to be exact.

So always round ever calculation result to (perhaps the nearest) integer value ...

and store your money as 'cents' ... then can print formated (string?) output ... where you show the 2 decimal places ... with the value shown as dollars.

you could code it: `while (rem_bal > 0.0)`

you could code it: while (rem_bal > 0.0)

Yes ... but then one still is using a 'double' to hold money ...

and cross-checks may NOT add to exact cent ...

For working with money, always best to use exact (large) whole numbers.

For working with money, always best to use exact (large) whole numbers.

Oddly enough, I just chastized a customer for implementing financial amounts as single precision floating point in a web service they wrote that one of our applications calls. The bug report was precision loss changing the values.

*sigh*

This is what I have now but I am still having an unending program

``````//Program to show calculation of interest on loan
#include <iostream>      // for cin, cout
#include <string>

using namespace std;
const double ANN_INTEREST=0.05;         //simple annual interest rate of 5%

int main()
{
double ann_interest, interest_rate, total_interest_paid=0.0, month_interest=(1.0/12.0);     //declaration of loan interest rates
float payment, loan, monthly_payment, interest_paid, rem_bal;                       //monetary values
int payment_count=1;                //declaration of the payment serial numbers

// Welcome message
cout<< "\$\$-\$+\$-\$+\$-\$+\$-\$+\$-\$+\$-\$\$\n"
<< "\$\$                     \$\$\n"
<< "... Topiloe's Bank ... \$\$\n"
<< "\$\$                     \$\$\n"
<< "\$\$-\$+\$-\$+\$-\$+\$-\$+\$-\$+\$-\$\$\n";

cout<<"What is the amount of your loan? ";
cin>>loan;
cout<<"What is the interest rate? ";
cin>>interest_rate;
cout<<endl;
monthly_payment=loan*ANN_INTEREST;
cout<<"Monthly payment on a loan of "<<loan<<" is "<<monthly_payment<<endl;
cout<<endl;
cout<<"Payment Details: \n";
cout<<"Payment Number\t"<<"Interest Paid\t"<<"Remaining Balance\n";
cout<<"--------------\t"<<"-------------\t"<<"-----------------\n";
payment=(20.0/100.0)*loan;

do
{
interest_paid=(interest_rate/100.0)*loan*month_interest;
rem_bal=payment-interest_paid;
cout<<payment_count<<"\t\t"<<interest_paid<<"\t\t"<<rem_bal<<endl;
payment_count++;
loan=rem_bal;
total_interest_paid=+interest_paid;
}
while (rem_bal>=0.0);
cout<<"Total interest paid is "<<total_interest_paid<<endl;

return 0;
}
``````

Your calculations are incorrect. I changed all doubles to floats so that the compiler wouldn't complain about assigning doubles to floats. And I removed rem_amt variable because it isn't needed.

You need to fix up the last payment because it may be too much, which puts remaining balance negative.

``````#include <iostream>      // for cin, cout
#include <string>

using namespace std;
const float ANN_INTEREST = 0.05F;         //simple annual interest rate of 5%

int main()
{
float interest_rate, total_interest_paid = 0.0F, month_interest = (1.0F / 12.0F);     //declaration of loan interest rates
float payment, loan, monthly_payment, interest_paid;                       //monetary values
int payment_count = 1;                //declaration of the payment serial numbers

// Welcome message
cout << "\$\$-\$+\$-\$+\$-\$+\$-\$+\$-\$+\$-\$\$\n"
<< "\$\$                     \$\$\n"
<< "... Topiloe's Bank ... \$\$\n"
<< "\$\$                     \$\$\n"
<< "\$\$-\$+\$-\$+\$-\$+\$-\$+\$-\$+\$-\$\$\n";

cout << "What is the amount of your loan? ";
cin >> loan;
cout << "What is the interest rate? ";
cin >> interest_rate;
cout << endl;
monthly_payment = loan*ANN_INTEREST;
cout << "Monthly payment on a loan of " << loan << " is " << monthly_payment << endl;
cout << endl;
cout << "Payment Details: \n";
cout << "Payment Number\t" << "Payment Amt\t" << "Interest Paid\t" << "Remaining Balance\n";
cout << "--------------\t" << "-------------\t" << "-----------------\n";
payment = (20.0F / 100.0F)*loan;
interest_rate /= 100.0F;
interest_rate /= 12.0F;
do
{
interest_paid = interest_rate*loan;
loan -= (payment - interest_paid);
cout << payment_count << "\t\t" << payment << "\t" << interest_paid << "\t\t" << loan << endl;
//cin.get();
payment_count++;
total_interest_paid = +interest_paid;
} while (loan >= 0.0);
cout << "Total interest paid is " << total_interest_paid << endl;

return 0;
}
``````

Do you think it will make sense if I make use argument passing for this work?

You could use functions to take in (and prompt and validate) all user input ... this would make your code more robust and modular.

For example, to prompt and to loop until the user has input a valid double ...

``````double takeInDbl( const char* msg )
{
double val;
while( true )
{
cout << msg << flush;
if( cin >> val && cin.get() == '\n' )
break;
else
{
cout << "Invalid input ... numbers only!\n";
cin.clear(); // clear error flasgs
cin.sync(); // 'flush' cin stream ...
}
}
return val;
}

// ... then later ...

// prompt and loop until valid double was input
double rate = takeInDbl( "PLease input rate: " );

// rest of code ...
``````

Sorry I don't understand this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.