# Problem I having issues with my program and don't seem to be able to work them out,can anyone straten me out here.

I have had alot of help on this but I have unfortunettly not been able to make it work yet.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
#include <stdlib.h>

using namespace std;
struct Choice
{
int    years;
double rate;
};

int main() {
//declare variables
float loan = 0;
float rate = 0;
float years = 0;
float balance = 0;
float term = 0;
int totalMonths = 0;
float payment = 0;
char again = 'n';
int choice = -1;

static const Choice menu[] = { {7, 5.35}, {15, 5.5}, {30, 5.75} };

do {

//User inputs loan information
cout << "Loan amount: \$";
cin >> loan;

int choice = -1;
while (-1 == choice)
{
cout << "Choose loan details:\n";
cout << "(1)\tInterset rate = " << menu[0].rate << "%, term = " << menu[0].years << " years\n";
cout << "(2)\tInterset rate = " << menu[1].rate << "%, term = " << menu[1].years << " years\n";
cout << "(3)\tInterset rate = " << menu[2].rate << "%, term = " << menu[2].years << " years\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
break;
case 2:
break;
case 3:
break;

default:
cout << "Invalid choice\n";
choice = -1;
}  //Display payment amounts
cout << setw(10) << (month);
cout << setw(10) << minterest;
cout << setw(10) << principal;
cout << setw(10) << balance << endl;
}// end if

// pause output every twelve lines, except for the final output
if (month % 21 == 0 && month % totalMonths != 0)
{
//cout << "\nPress any key to continue . . .\n";
//program flow stops here until a key is pressed on the keyboard
//getch();
system("pause");
system("cls");
cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"-------------------------------------\n";
}// end if
}// end for

} while ((again != 'n') && (again != 'N'));

do
{
cout << "Would you like to try another amount (Y/N):";
cin >> again;
cout << '\n';
} while ((again != 'n') && (again != 'N') && (again != 'y') &&
(again != 'Y'));

} while ((again != 'n') && (again != 'N'));
return 0;

}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi Sunami32,
> I have had alot of help on this but I have unfortunettly not been able
> to make it work yet.

How exactly does it not work.
Please give us more details so we can better assist you.

David Maisonave (Axter)
Cheers!
Author Commented:
The problem is it doesn't show the months, interest,principle and balance.
Commented:
>>The problem is it doesn't show the months, interest,principle and balance.
Exactly what does it show?
Can you copy and paste your output here, so we can get the full picture?
Commented:
cout << setw(10) << (month);
cout << setw(10) << minterest;
cout << setw(10) << principal;
cout << setw(10) << balance << endl;

I don't see any of the above variables declared in your code.
So the code you've provided does not compile.

Do you have code that will compile?

If you do have compilable code, please provide it, so that we can better assist you.

Author Commented:
I'm still haveing a few bugs before it will compile and that is where i need the help. How do I set this up to show them the payment every month interest they paid and the left over balance of the loan in intervals of 12 line scrolls.
Author Commented:
yes here you go

#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
#include <stdlib.h>
using namespace std;

int main() {
//declare variables
double loan = 0.0;
double rate = 0.0;
int years = 0;
double balance = 0.0;
double term = 0.0;
int totalMonths = 0;
double payment = 0.0;
char again = 'n';
double minterest, principal;
int month;
do
{
//User inputs loan information
cout << "Loan amount: \$";
cin >> loan;
cout << "Annual Interest Rate(input % in decimal (format example 5.75):";
cin >> rate;
cout << "Years of Loan: ";
cin >> years;
rate /=100;
//Calculation for monthly payment
term = pow((1+rate /12.0), 12.0 * years);
totalMonths = 12 * years;
payment = (loan * rate / 12.0 * term) / (term - 1.0);

//Display monthly payment
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint | ios::left);
cout <<"Monthly payment: \$" <<payment <<endl;

cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"---------------------------------------\n";

//Produce a listing for each month
balance = loan;
for (month=1; month<totalMonths + 1; month++)
{

if (balance > 0)
{
//Calculate monthly interest
minterest = rate / 12 * balance;
principal = payment - minterest;
balance -= principal;
if (balance < 0)
balance = 0;

//Display payment amounts
cout << setw(10) << (month);
cout << setw(10) << minterest;
cout << setw(10) << principal;
cout << setw(10) << balance << endl;
}// end if

// pause output every twelve lines, except for the final output
if (month % 21 == 0 && month % totalMonths != 0)
{
//cout << "\nPress any key to continue . . .\n";
//program flow stops here until a key is pressed on the keyboard
//getch();
system("pause");
system("cls");
cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"-------------------------------------\n";
}// end if
}// end for
do
{
cout << "Would you like to try another amount (Y/N):";
cin >> again;
cout << '\n';
} while ((again != 'n') && (again != 'N') && (again != 'y') &&
(again != 'Y'));

} while ((again != 'n') && (again != 'N'));
return 0;
}
Author Commented:
what I'm trying to do is make the code use the array to allow the user to choose from three selected static const Choice menu[] = { {7, 5.35}, {15, 5.5}, {30, 5.75} };

Author Commented:
year interest,year interest,year,interest.
Commented:
>>I'm still haveing a few bugs before it will compile and that is where i need the help. How do I set this up to show them the payment every month interest they paid and the left over balance of
>>the loan in intervals of 12 line scrolls.

So your code does not compile.  This is very important information to give, when you post a C++ question.
We need to know if it compiles, if it links, and if it runs.

You first need to declare the variables.
Commented:
I just gave it a shot, it works fine:

Loan amount: \$150000
Annual Interest Rate(input % in decimal (format example 5.75):5.75
Years of Loan: 30
Monthly payment: \$875.36

Month     Interest  Principal Balance
---------------------------------------
1         718.75    156.61    149843.39
2         718.00    157.36    149686.03
3         717.25    158.11    149527.92
4         716.49    158.87    149369.05
5         715.73    159.63    149209.41
6         714.96    160.40    149049.02
7         714.19    161.17    148887.85
8         713.42    161.94    148725.91
9         712.64    162.71    148563.20
10        711.87    163.49    148399.70
11        711.08    164.28    148235.43
12        710.29    165.06    148070.36
13        709.50    165.86    147904.51
14        708.71    166.65    147737.86
15        707.91    167.45    147570.41
16        707.11    168.25    147402.16
17        706.30    169.06    147233.10
18        705.49    169.87    147063.23
19        704.68    170.68    146892.55
20        703.86    171.50    146721.05
21        703.04    172.32    146548.73
...

What other output did you expect?
Commented:
from your last compilable code to the code in the question you have deleted a heap of neccessary lines.

First you have deleted the declaration of month, principle and interest:

double minterest, principal;
int month;

Second you have deleted the code that performs your interest calculations:

//Calculation for monthly payment
term = pow((1+rate /12.0), 12.0 * years);
totalMonths = 12 * years;
payment = (loan * rate / 12.0 * term) / (term - 1.0);

//Display monthly payment
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint | ios::left);
cout <<"Monthly payment: \$" <<payment <<endl;

cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"---------------------------------------\n";

//Produce a listing for each month
balance = loan;
for (month=1; month<totalMonths + 1; month++)
{

if (balance > 0)
{
//Calculate monthly interest
minterest = rate / 12 * balance;
principal = payment - minterest;
balance -= principal;
if (balance < 0)
balance = 0;

Commented:
>>How do I set this up to show them the payment every month interest they paid and the left over balance of
>>the loan in intervals of 12 line scrolls.

You have a type in your code:

>>  if (month % 21 == 0 && month % totalMonths != 0)

will pause your output every 21 months...
Author Commented:
Okay so I went back and try to combine the two programs properly I still having compile issues I bridged the program with a outher to allow the user the abillity to enter there own amount. I 'm sttill taking errors on it thoe dealing with the calculation .

#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
#include <stdlib.h>
using namespace std;

struct Choice
{
int    years;
double rate;
};
int main() {
//declare variables
float loan = 0;
float rate = 0;
float years = 0;
float balance = 0;
float term = 0;
int totalMonths = 0;
float payment = 0;
char again = 'n';
int choice = -1;

static const Choice menu[] = { {7, 5.35}, {15, 5.5}, {30, 5.75} };

do {

//User inputs loan information
cout << "Loan amount: \$";
cin >> loan;
int choice = -1;
while (-1 == choice)
{
cout << "Choose loan details:\n";
cout << "(1)\tInterset rate = " << menu[0].rate << "%, term = " << menu[0].years << " years\n";
cout << "(2)\tInterset rate = " << menu[1].rate << "%, term = " << menu[1].years << " years\n";
cout << "(3)\tInterset rate = " << menu[2].rate << "%, term = " << menu[2].years << " years\n";
cout << "(4)\tOther\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
cout << "Annual Interest Rate(input % in decimal format example not 5.75 but .0575):";
cin >> rate;
cout << "Years of Loan: ";
cin >> years;
break;
default:
cout << "Invalid choice\n";
choice = -1;
}
}
do
{

//Calculation for monthly payment
term = pow((1+rate /12.0), 12.0 * years);
totalMonths = 12 * years;
payment = (loan * rate / 12.0 * term) / (term - 1.0);

//Display monthly payment
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint | ios::left);
cout <<"Monthly payment: \$" <<payment <<endl;

cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"---------------------------------------\n";

//Produce a listing for each month
balance = loan;
for (month=1; month<totalMonths + 1; month++)
{
// avoid declaring variables inside a loop. It adds
// unecessary creation and destruction of variables each time
// the loop runs.  float minterest, principal;

if (balance > 0)
{
//Calculate monthly interest
minterest = rate / 12 * balance;
principal = payment - minterest;
balance -= principal;
if (balance < 0)
balance = 0;

//Display payment amounts
cout << setw(10) << (month);
cout << setw(10) << minterest;
cout << setw(10) << principal;
cout << setw(10) << balance << endl;
}// end if

// pause output every twelve lines, except for the final output
if (month % 21 == 0 && month % totalMonths != 0)
{
//cout << "\nPress any key to continue . . .\n";
//program flow stops here until a key is pressed on the keyboard
//getch();
system("pause");
system("cls");
cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"-------------------------------------\n";
}// end if
}// end for
do
{
cout << "Would you like to try another amount (Y/N):";
cin >> again;
cout << '\n';
} while ((again != 'n') && (again != 'N') && (again != 'y') &&
(again != 'Y'));

} while ((again != 'n') && (again != 'N'));
return 0;
}
Commented:
OK, you are still missing decaraltions:

double minterest, principal;
int month;

ALso you have an extra

do {

before

//Calculation for monthly payment
term = pow((1+rate /12.0), 12.0 * years);
totalMonths = 12 * years;
payment = (loan * rate / 12.0 * term) / (term - 1.0);

Also you still have

// pause output every twelve lines, except for the final output
if (month % 21 == 0 && month % totalMonths != 0)

// pause output every twelve lines, except for the final output
if (month % 12 == 0 && month % totalMonths != 0)

Author Commented:
Friends I'm sorry to jump here,But I have to get some sleep I been up for about 24 hours at the hospital with family,If someone could tweek this for me please do.If not I'll, be back tomorrow when I can spell again,or all this C++ makes more since thank's and I'll see you all in 15 hours.
Commented:
The problem are missing declarations and the loops - the following

#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
#include <stdlib.h>
using namespace std;

struct Choice
{
int    years;
double rate;
};
int main() {
//declare variables
float loan = 0;
float rate = 0;
float years = 0;
float balance = 0;
float term = 0;
int totalMonths = 0;
float payment = 0;
char again = 'n';
int choice = -1;
double minterest, principal;
int month;

static const Choice menu[] = { {7, 5.35}, {15, 5.5}, {30, 5.75} };

do {

//User inputs loan information
cout << "Loan amount: \$";
cin >> loan;
int choice = -1;
while (-1 == choice)
{
cout << "Choose loan details:\n";
cout << "(1)\tInterset rate = " << menu[0].rate << "%, term = " << menu[0].years << "

years\n";
cout << "(2)\tInterset rate = " << menu[1].rate << "%, term = " << menu[1].years << "

years\n";
cout << "(3)\tInterset rate = " << menu[2].rate << "%, term = " << menu[2].years << "

years\n";
cout << "(4)\tOther\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
cout << "Annual Interest Rate(input % in decimal format example not 5.75 but .0575):";
cin >> rate;
cout << "Years of Loan: ";
cin >> years;
break;
default:
cout << "Invalid choice\n";
choice = -1;
}
}
do // do2
{

//Calculation for monthly payment
term = pow((1+rate /12.0), 12.0 * years);
totalMonths = 12 * years;
payment = (loan * rate / 12.0 * term) / (term - 1.0);

//Display monthly payment
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint | ios::left);
cout <<"Monthly payment: \$" <<payment <<endl;

cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"---------------------------------------\n";

//Produce a listing for each month
balance = loan;
for (month=1; month<totalMonths + 1; month++)
{
// avoid declaring variables inside a loop. It adds
// unecessary creation and destruction of variables each time
// the loop runs.  float minterest, principal;

if (balance > 0)
{
//Calculate monthly interest
minterest = rate / 12 * balance;
principal = payment - minterest;
balance -= principal;
if (balance < 0)
balance = 0;

//Display payment amounts
cout << setw(10) << (month);
cout << setw(10) << minterest;
cout << setw(10) << principal;
cout << setw(10) << balance << endl;
}// end if

// pause output every twelve lines, except for the final output
if (month % 21 == 0 && month % totalMonths != 0)
{
//cout << "\nPress any key to continue . . .\n";
//program flow stops here until a key is pressed on the keyboard
//getch();
system("pause");
system("cls");
cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"-------------------------------------\n";
}// end if
}// end for

//      do
//          {
//         cout << "Would you like to try another amount (Y/N):";
//         cin >> again;
//         cout << '\n';
} while ((again != 'n') && (again != 'N') && (again != 'y') &&
(again != 'Y'));

} while ((again != 'n') && (again != 'N'));

return 0;
}

outputs

Loan amount: \$150000
Choose loan details:
(1)     Interset rate = 5.35%, term = 7 years
(2)     Interset rate = 5.5%, term = 15 years
(3)     Interset rate = 5.75%, term = 30 years
(4)     Other
Enter choice: 3
Monthly payment: \$875.36

Month     Interest  Principal Balance
---------------------------------------
1         718.75    156.61    149843.39
2         718.00    157.36    149686.03
3         717.25    158.11    149527.92
4         716.49    158.87    149369.05
5         715.73    159.63    149209.42
6         714.96    160.40    149049.03
7         714.19    161.17    148887.86
8         713.42    161.94    148725.92
9         712.65    162.71    148563.20
10        711.87    163.49    148399.70
11        711.08    164.28    148235.42
12        710.29    165.06    148070.36
13        709.50    165.86    147904.50
14        708.71    166.65    147737.84
15        707.91    167.45    147570.39
16        707.11    168.25    147402.14
17        706.30    169.06    147233.08
18        705.49    169.87    147063.20
19        704.68    170.68    146892.52
20        703.86    171.50    146721.02
21        703.04    172.32    146548.69

Experts Exchange Solution brought to you by