Link to home
Start Free TrialLog in
Avatar of mhho
mhho

asked on

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'
Please help....



#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;

      //Display report header
      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");

            //Re-display report header
            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;
}
Avatar of sunnycoder
sunnycoder
Flag of India image

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
Avatar of mhho
mhho

ASKER

I am confused  can you show me  what did you mean
Thanks
Hi mhho,

>    int totalMonths = 0;
should be

double totalMonths = 0;

Cheers!
Sunnycoder
Avatar of mhho

ASKER

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

Hi mhho,

C++ is case sensitive .. double not Double

Cheers!
Sunnycoder
Avatar of mhho

ASKER

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;

      //Display report header
      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");

            //Re-display report header
            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;
}
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 ...

Avatar of mhho

ASKER

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 {
      //Display program header
      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;

      //Display report header
      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");

            //Re-display report header
            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'));
}
Avatar of mhho

ASKER

Can someone  help out .......urgent
Thanks
Check your equations ....

      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
SOLUTION
Avatar of vishwa_karthick
vishwa_karthick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mhho

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mhho

ASKER

for (int i=1;i<=n;i++) //loop till total number of months .Why that was initialized to '1'
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