• Status: Solved
• Priority: Medium
• Security: Public
• Views: 335

# C++ ERROR

The complie give me a n error :
In function `int main()':
9: warning: assignment to `int' from `double'
9: warning: argument to `int' from `double'

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

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';
do {
//User inputs loan information
cout << "Loan amount: \$";
cin >> loan;
cout << "Annual Interest Rate(input % in decimal format):";
cin >> rate;
cout << "Years of Loan: ";
cin >> years;

//Calculation for monthly payment
term = pow((1+rate / 12.0), 12.0 * years);
totalMonths = 12.0 * 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 (int month=1; month<totalMonths + 1; month++) {
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;
}

// pause output every twelve lines, except for the final output
if (month % 12 == 0 && month % totalMonths != 0) {
cout << "\nPress any key to continue . . .\n";
//program flow stops here until a key is pressed on the keyboard
getch();

//Clear output screen
system("CLS");
//          system("PAUSE");

cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"-------------------------------------\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;
}
0
mhho
• 7
• 7
2 Solutions

Commented:
Hi mhho,

> totalMonths = 12.0 * years;
totalMonths is an int ... the expression on the RHS would evaluate to a double !!!

Compiler is issuing warning because you are assigning a double to an int and this can lead to potential problems ...

Declare totalMonths as a double.

Cheers!
Sunnycoder
0

Author Commented:
I am confused  can you show me  what did you mean
Thanks
0

Commented:
Hi mhho,

>    int totalMonths = 0;
should be

double totalMonths = 0;

Cheers!
Sunnycoder
0

Author Commented:
I did change it to double totalMonths = 0;
But still have these error:

s.cpp: In function `int main()':
s.cpp:15: `Double' undeclared (first use this function)
s.cpp:15: (Each undeclared identifier is reported only once for each function
it appears in.)
s.cpp:15: parse error before `=' token
s.cpp:29: `totalMonths' undeclared (first use this function)
s.cpp:96:2: warning: no newline at end of file

0

Commented:
Hi mhho,

C++ is case sensitive .. double not Double

Cheers!
Sunnycoder
0

Author Commented:
Still error
s.cpp: In function `int main()':
s.cpp:67: invalid operands of types `int' and `double' to binary `operator%'

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

int main() {
//declare variables
float loan = 0;
float rate = 0;
float years = 0;
float balance = 0;
float term = 0;
double totalMonths = 0;
float payment = 0;
char again = 'n';
do {
//User inputs loan information
cout << "Loan amount: \$";
cin >> loan;
cout << "Annual Interest Rate(input % in decimal format):";
cin >> rate;
cout << "Years of Loan: ";
cin >> years;

//Calculation for monthly payment
term = pow((1+rate / 12.0), 12.0 * years);
totalMonths = 12.0 * 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 (double month=1; month<totalMonths + 1; month++) {
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;
}

// pause output every twelve lines, except for the final output
if (month % 12 == 0 && month % totalMonths != 0) {
cout << "\nPress any key to continue . . .\n";
//program flow stops here until a key is pressed on the keyboard
getch();

//Clear output screen
system("CLS");
//          system("PAUSE");

cout << endl;
cout << setw(10) <<"Month";
cout << setw(10) <<"Interest";
cout << setw(10) <<"Principal";
cout << setw(10) <<"Balance" << endl;
cout << setw(10) <<"-------------------------------------\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;
}
0

Commented:
you cant use a double and an int with % operator ...

Truncate or round off totalMonths before using it in that expression .... floor() ciel() might be functions of interest ...

0

Author Commented:
Ok!  I modified the code it complied but the out put is not show correct

here the code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>

using namespace std;

int main() {
//declare variables
float loan = 0;
float rate = 0;
float years = 0;
float balance = 0;
float term = 0;
float payment = 0;
int month = 12;
int totalMonths = 0;

char again;

do {
cout << "\t\t***************************************\n";
cout << "\t\t**                    Mortgage Calculator                     **\n";
cout << "\t\t***************************************\n";
cout << "\n";
cout << "\n";

//User inputs loan information
cout << "Loan amount: \$";
cin >> loan;
cout << "Annual Interest Rate(input % in decimal format):";
cin >> rate;
cout << "Years of Loan: ";
cin >> years;

//Equation for monthly payment
term = pow((1+rate / 12.0), 12.0 * 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 (int month=1; month<month + 1; month++) {
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;
}

// pause output every twelve lines, except for the final output
if (month % 12 == 0 && month % (int)term != 0) {
cout << "\nPress any key to continue . . .\n";
//program flow stops here until a key is pressed on the keyboard
getch();

//Clear output screen
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";
}
}

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

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

Author Commented:
Can someone  help out .......urgent
Thanks
0

Commented:

term = pow((1+rate / 12.0), 12.0 * years);
payment = (loan * rate / 12.0 * term) / (term - 1.0);

These seem to be incorrect ....
http://www.hughchou.org/calc/formula.html
0

Commented:
The reason for this is the statement
totalMonths = 12.0 * years;
In this statement, since 12.0 is double value and years is float, the result is also double. When you try to assign this value to totalMonths which is an int, you are getting the error.

You may want to try one of the two solutions:
Solution 1: Typecast the solution to integer
totalMonths = (int)(12.0 * years);

Solution 2: Make the variable totalMonths as double:
double totalMonths;

I would use the second solution since this is way there is no loss of data.

Thanks and regards,
Karthick S.
0

Author Commented:
Ok! one more thing I've  saw one of the logic in the book it  say I can use:
if(i%12 == 0 &&i !=n)//loop so that user can see only 12 entries at a time on screen

'i' is a standard loop counter used that was initialized to '1'. I can't figure out the logic of it though. Any insight you can give ?

Thanks
0

Commented:
> if(i%12 == 0 &&i !=n)
This condition would evaluate to true only when
a)  i is a multiple of 12
AND
b) i is not equal to n

>//loop so that user can see only 12 entries at a time on screen
The condition looks more like user can see every 12th entry ... Can you show us more code?
0

Author Commented:
for (int i=1;i<=n;i++) //loop till total number of months .Why that was initialized to '1'
0

Commented:
terminating ocndition is i<=n instead of i<n .. so the loop would run the same number of times as i=0;i<n;i++

i *may* have been initialized to 1 to accomodate some calculations in the loop
0

## Featured Post

• 7
• 7
Tackle projects and never again get stuck behind a technical roadblock.