Solved

# C++ ERROR

Posted on 2006-05-06
322 Views
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
Question by:mhho

LVL 45

Expert Comment

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 Comment

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

LVL 45

Expert Comment

Hi mhho,

>    int totalMonths = 0;
should be

double totalMonths = 0;

Cheers!
Sunnycoder
0

Author Comment

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

LVL 45

Expert Comment

Hi mhho,

C++ is case sensitive .. double not Double

Cheers!
Sunnycoder
0

Author Comment

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

LVL 45

Expert Comment

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 Comment

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 Comment

Can someone  help out .......urgent
Thanks
0

LVL 45

Expert Comment

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

LVL 1

Assisted Solution

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 Comment

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

LVL 45

Accepted Solution

> 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 Comment

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

LVL 45

Expert Comment

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

### Suggested Solutions

Exchange and Third Party application. 8 45
post4 challenge 28 69
zeroMAx challenge 20 66
Visual xHarbour 1 47
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…