By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,462 Members | 799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,462 IT Pros & Developers. It's quick & easy.

C++ Mortgage Schedule...Please help!!

P: 11
I am trying to get my program to compile, but it will not complete. At line 79 it states, cannot convert 'float()()' to 'float' in assignment. Here is my code.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>        
  2. #include <iomanip>
  3. #include <cmath>    
  4. #include <cctype>
  5. #include <stdlib.h>
  6. #include <sstream>    
  7.  
  8. using namespace std;
  9.  
  10. void MortSched(double, double, int);
  11.  
  12. //The structure below will contain the mortgage information
  13.  
  14. struct PaymentInfo
  15. {
  16.        float fMonPayment;
  17.        float fYears;
  18.        float fInterestRate;
  19.        float fLoanAmount;
  20.        };
  21.  
  22. int GetPayment();
  23. int LoopAgain();
  24. float UserInput();
  25. void GetPaymentInfo(PaymentInfo& payinfo);
  26. void DeterminePayment(PaymentInfo& payinfo);
  27. void ShowPayment(PaymentInfo& payinfo);
  28. char iResponse;
  29.  
  30. //The following section of code will validate user input.  We only want the user to
  31. //be able to enter numbers or the decimal.
  32.  
  33. class nondigit
  34. {
  35.       public:
  36.              bool operator() (char c)
  37.              {
  38.                   if (c =='.')
  39.                   {
  40.                         return !1;
  41.                         }
  42.                         else
  43.                         {
  44.                             return !isdigit(c);
  45.                             }
  46.                             }
  47.                             };
  48.  
  49. //This next section of code will accept the user input and validate all input
  50. //the user enters.  The main function here is to validate all user input.
  51.  
  52. float UserInput()
  53. {
  54.       float output;
  55.       string input;
  56.       cin >> input;
  57.       if (find_if(input.begin(), input.end(), nondigit()) == input.end())
  58.       {
  59.                                  stringstream mc(input);
  60.                                  mc >> output;
  61.                                  }
  62.                                  else
  63.                                  {
  64.                                      cout << input << " is not a valid number! Please try again!\n";
  65.                                      output = UserInput;
  66.                                      }
  67.                                      return output;
  68.                                      }
  69.  
  70. //The next section of code prompts the user to enter the amounts for the mortgage calculator
  71.  
  72. void GetPaymentInfo(PaymentInfo& payinfo)
  73. {
  74.      cout << "********************Mortgage Calculator********************\n";
  75.      cout << "What is the amount of the mortgage?  For example 200000\n";
  76.      payinfo.fLoanAmount = UserInput();
  77.      cout << "What is the amount of years the mortgage will be financed? For example 30\n";
  78.      payinfo.fYears = UserInput();
  79.      cout << "What is the interest rate? For example 5.75\n";
  80.      payinfo.fInterestRate = UserInput();
  81.      }
  82.  
  83. //The following code calculates the monthly payment amount based on the users input
  84.  
  85. void DeterminePayment(PaymentInfo& payinfo)
  86. {
  87.      payinfo.fMonPayment =
  88.      {
  89.                          payinfo.fLoanAmount *
  90.                          {
  91.                                              (payinfo.fInterestRate / 100) / 12 * pow((payinfo.fInterestRate / 100) / 12 + 1, (payinfo.fYears * 12)))
  92.                                              /
  93.                                              (pow((payinfo.fInterestRate / 100) / 12 + 1, (payinfo.fYears * 12))-1
  94.                                              }
  95.                                              }
  96.                                              };
  97.                                              }
  98.  
  99. //The following code displays the payment amount based on the users input
  100.  
  101. void ShowPayment(PaymentInfo& payinfo)
  102. {
  103.      cout << "Amount of mortgage = $\n" << payinfo.fLoanAmount <<;
  104.      cout << "Years financed = \n" << payinfo.fYears <<;
  105.      cout << "Interest Rate = \n" << payinfo.fInterestRate <<;
  106.      cout << "Monthly Payment Amount = $\n" << payinfo.MonPayment <<;
  107.      }
  108.  
  109. //The next section will allow the user to run the program again or exit the program
  110.  
  111. int LoopAgain()
  112. {
  113.     int loop = 0;
  114.     char mychar[7];
  115.     cout << "To run this program again, please press 1, and then press enter.\n";
  116.     cout << "To exit this program, please press 2, and then press enter.\n";
  117.     cin >> ws;
  118.     cin.getline(mychar,7);
  119.     if (mychar[0] == '1')
  120.     {
  121.                   cout << "Thank you for choosing option 1.  This program will run again.\n";
  122.                   run = 1;
  123.                   }
  124.                   else
  125.                   {
  126.                       cout << "Thank you for choosing option 2.  This program will now exit.\n";
  127.                       run = 0;
  128.                       }
  129.                       return run;
  130.                       }
  131.  
  132. int runGetPayment()
  133. {
  134.     float fMonPayment = 0.00;
  135.     PaymentInfo structPaymentInfo;
  136.     GetPaymentInfo(structPaymentInfo);
  137.     DeterminePayment(structPaymentInfo);
  138.     ShowPayment(structPaymentInfo);
  139.     int again = LoopAgain();
  140.     return again;
  141. }
  142.  
  143. //We will offer the user the option to view the amortization schedule
  144.  
  145. cout.setf(ios::showpoint | ios::fixed);
  146. cout.precision(2);
  147.  
  148. cout << "Would you like to view the Amortization Schedule? Press Y or N.\n";
  149. iResponse = getch();
  150. if(iResponse == 'Y' || iResponse == 'y')
  151. {
  152.    MortSched(fLoanAmount, finterestRate, fYears);
  153.             iResponse = '\n';   
  154.             }
  155.             else
  156.             {   
  157.                 iResponse = '\n';
  158.         }    
  159.         cout << endl << "Would you like to run this again? Press Y or N. ";
  160.         iResponse = getch();
  161.  
  162.     }while((iResponse == 'Y') || (iResponse == 'y'));
  163.  
  164.     return 0;
  165. }
  166. //The following code will calculate the amortization schedule.
  167.  
  168. void MortSched(double fLoanAmount, double fInterestRate, int fYears)
  169. {
  170.     double Balance, PaidInt, MonPayment, LoanAmount, Principle;
  171.     int lineCount = 0;
  172.     cout.setf(ios::left);
  173.     MonPayment = calcPayment(&LoanAmount, &InterestRate, &Years);
  174.     Balance = LoanAmount;      
  175.     for (int InstallNumber = 1; InstallNumber <= Years; ++InstallNumber)
  176.     {
  177.         if (lineCount == 0)
  178.         {
  179.             system("CLS");
  180.             cout << "Payment          Loan          Principle          Interest" << endl;
  181.             cout << "No.              Balance       Payment            Payment" << endl;
  182.             cout << "**********************************************************" << endl; 
  183.         }
  184.  
  185.         PaidInt = Balance * (InterestRate / (12 * 100));
  186.         LoanAmount = MonPayment - PaidInt;
  187.         Principle = Balance - Principle;
  188.         cout << setw(12) << InstallNumber
  189.              << setw(15) << Balance
  190.                << setw(15) << MonPayment
  191.              << setw(15) << PaidInt << endl;
  192.         Balance = Principle;        
  193.         if (lineCount==12)
  194.         {
  195.             system("pause");
  196.             lineCount = 0;
  197.                     } 
  198.                            else
  199.         {
  200.             lineCount++;
  201.         }
  202.     }
  203.  
  204.     cout.unsetf(ios::left);
  205.     return;
  206. }
  207.  
Thanks for all the help!!
Sep 18 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
In function UserInput

output = UserInput;

should be

output = UserInput();


It would be more normal to produce this behaviour(rejecting a bad input value) using a loop rather than recursion.
Sep 18 '06 #2

P: 11
I have almost got everything worked out to the end, but can you please explain to me why it is hanging on line 170?
Expand|Select|Wrap|Line Numbers
  1. MortSched(fLoanAmount, fInterestRate, fYears);
It says that fLoanAmount and the others are not declared, but I declared them in my structure at the beginning of the program? I am REALLY new to programming and so far I have been treated like an idiot from all the other forums I have asked. I am not a software person at all. I am a hardware oriented person, but of course this is one of my degree requirements. My amended code is as follows:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>        
  2. #include <iomanip>
  3. #include <cmath>    
  4. #include <cctype>
  5. #include <stdlib.h>
  6. #include <sstream>    
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. void MortSched(double, double, int);
  12.  
  13. //The structure below will contain the mortgage information
  14.  
  15. struct PaymentInfo
  16. {
  17.        float fMonPayment;
  18.        float fYears;
  19.        float fInterestRate;
  20.        float fLoanAmount;
  21. };
  22.  
  23. int GetPayment();
  24. int LoopAgain();
  25. float UserInput();
  26. void GetPaymentInfo(PaymentInfo& payinfo);
  27. void DeterminePayment(PaymentInfo& payinfo);
  28. void ShowPayment(PaymentInfo& payinfo);
  29. char iResponse;
  30.  
  31. //The following section of code will validate user input.  We only want the user to
  32. //be able to enter numbers or the decimal.
  33.  
  34. class nondigit
  35. {
  36.       public:
  37.              bool operator() (char c)
  38.              {
  39.                   if (c =='.')
  40.                   {
  41.                         return !1;
  42.                   }
  43.                         else
  44.                         {
  45.                             return !isdigit(c);
  46.                         }
  47.              }
  48. };
  49.  
  50. //This next section of code will accept the user input and validate all input
  51. //the user enters.  The main function here is to validate all user input.
  52.  
  53. float UserInput()
  54. {
  55.       float output;
  56.       string input;
  57.       cin >> input;
  58.       if (find_if(input.begin(), input.end(), nondigit()) == input.end())
  59.       {
  60.                                  stringstream mc(input);
  61.                                  mc >> output;
  62.       }
  63.                                  else
  64.                                  {
  65.                                      cout << input << " is not a valid number! Please try again!\n";
  66.                                      output = UserInput();
  67.                                   }
  68.                                      return output;
  69. }
  70.  
  71. //The next section of code prompts the user to enter the amounts for the mortgage calculator
  72.  
  73. void GetPaymentInfo(PaymentInfo& payinfo)
  74. {
  75.      cout << "********************Mortgage Calculator********************\n";
  76.      cout << "What is the amount of the mortgage?  For example 200000\n";
  77.      payinfo.fLoanAmount = UserInput();
  78.      cout << "What is the amount of years the mortgage will be financed? For example 30\n";
  79.      payinfo.fYears = UserInput();
  80.      cout << "What is the interest rate? For example 5.75\n";
  81.      payinfo.fInterestRate = UserInput();
  82. }
  83.  
  84. //The following code calculates the monthly payment amount based on the users input
  85.  
  86. void DeterminePayment(PaymentInfo& payinfo)
  87. {
  88.      payinfo.fMonPayment = (0);
  89.      {
  90.                          payinfo.fLoanAmount = (0);
  91.                          {
  92.                                              ((payinfo.fInterestRate / 100) / 12 * pow((payinfo.fInterestRate / 100) / 12 + 1, (payinfo.fYears * 12)))
  93.                                              /
  94.                                              (pow((payinfo.fInterestRate / 100) / 12 + 1, (payinfo.fYears * 12))-1);
  95.                          }
  96.       }
  97. }                                       
  98.  
  99. //The following code displays the payment amount based on the users input
  100.  
  101. void ShowPayment(PaymentInfo& payinfo)
  102. {
  103.      cout << "Amount of mortgage = $\n" << payinfo.fLoanAmount << endl;
  104.      cout << "Years financed = \n" << payinfo.fYears << endl;
  105.      cout << "Interest Rate = \n" << payinfo.fInterestRate << endl;
  106.      cout << "Monthly Payment Amount = $\n" << payinfo.fMonPayment << endl;
  107. }
  108.  
  109. //The next section will allow the user to run the program again or exit the program
  110.  
  111. int LoopAgain()
  112. {
  113.     int run = 0;
  114.     int loop = 0;
  115.     char mychar[7];
  116.     cout << "To run this program again, please press 1, and then press enter.\n";
  117.     cout << "To exit this program, please press 2, and then press enter.\n";
  118.     cin >> ws;
  119.     cin.getline(mychar,7);
  120.     if (mychar[0] == '1')
  121.     {
  122.                   cout << "Thank you for choosing option 1.  This program will run again.\n";
  123.                   run = 1;
  124.     }
  125.                   else
  126.                   {
  127.                       cout << "Thank you for choosing option 2.  This program will now exit.\n";
  128.                       run = 0;
  129.                   }
  130.                       return run;
  131. }
  132.  
  133. int runGetPayment()
  134. {
  135.     float fMonPayment = 0.00;
  136.     PaymentInfo structPaymentInfo;
  137.     GetPaymentInfo(structPaymentInfo);
  138.     DeterminePayment(structPaymentInfo);
  139.     ShowPayment(structPaymentInfo);
  140.     int again = LoopAgain();
  141.     return again;
  142. }
  143.  
  144. //We will offer the user the option to view the amortization schedule
  145. int main()
  146. {
  147. cout.setf(ios::fixed);
  148. cout.setf(ios::showpoint);
  149. cout.precision(2);
  150.  
  151.                   cout<< endl << "Would you like to view the Amortization Schedule? Press Y or N.\n";
  152.                   iResponse = getchar();
  153.  
  154. if(iResponse == 'Y' || iResponse == 'y')
  155.  
  156.    MortSched(fLoanAmount, fInterestRate, fYears);
  157.             iResponse = '\n';   
  158. }
  159.             else
  160.             {   
  161.                 iResponse = '\n';
  162.             }      
  163.         cout << endl << "Would you like to run this again? Press Y or N. ";
  164.         iResponse = getch();
  165.         do
  166.  
  167. }
  168.     while((iResponse == 'Y') || (iResponse == 'y'));
  169.  
  170.     return 0;
  171. }
  172. //The following code will calculate the amortization schedule.
  173.  
  174. void MortSched(double fLoanAmount, double fInterestRate, int fYears)
  175. {
  176.     double Balance, PaidInt, MonPayment, LoanAmount, Principle;
  177.     int lineCount = 0;
  178.     cout.setf(ios::left);
  179.     MonPayment = calcPayment(&LoanAmount, &InterestRate, &Years);
  180.     Balance = LoanAmount;      
  181.     for (int InstallNumber = 1; InstallNumber <= Years; ++InstallNumber)
  182.     {
  183.         if (lineCount == 0)
  184.         {
  185.             system("CLS");
  186.             cout << "Payment          Loan          Principle          Interest" << endl;
  187.             cout << "No.              Balance       Payment            Payment" << endl;
  188.             cout << "**********************************************************" << endl; 
  189.         }
  190.  
  191.         PaidInt = Balance * (InterestRate / (12 * 100));
  192.         LoanAmount = MonPayment - PaidInt;
  193.         Principle = Balance - Principle;
  194.         cout << setw(12) << InstallNumber
  195.              << setw(15) << Balance
  196.                << setw(15) << MonPayment
  197.              << setw(15) << PaidInt << endl;
  198.         Balance = Principle;        
  199.         if (lineCount==12)
  200.            {
  201.             system("pause");
  202.             lineCount = 0;
  203.             } 
  204.                            else
  205.         {
  206.             lineCount++;
  207.         }
  208. }
  209.  
  210.     cout.unsetf(ios::left);
  211.     return;
  212. }
Thanks for all your help!!
Sep 18 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
I have almost got everything worked out to the end, but can you please explain to me why it is hanging on line 170?
Expand|Select|Wrap|Line Numbers
  1. MortSched(fLoanAmount, fInterestRate, fYears);
It says that fLoanAmount and the others are not declared, but I declared them in my structure at the beginning of the program?
Declaring a structure does not declare data, it declares the form of the data. You then have to declare a variable of that structure type and then when you want to access members of the variable you have to use the variable name plus the member name.

Like this

Expand|Select|Wrap|Line Numbers
  1. /* Declare the structure */
  2. struct MyExampleStructure {
  3.     int iIntegerMember;
  4.     float fFloatingPointMember
  5. };
  6.  
  7. /* Declare a variable of type struct MyExampleStructure */
  8. struct MyExampleStructure mes;
  9.  
  10. /* Access the structure members of mes */
  11. mes.iIntegerMember = 10;
  12. mes.fFloatingPointMember = 1.618;
  13.  
You compiler complains about the variables in this code line
Expand|Select|Wrap|Line Numbers
  1. MortSched(fLoanAmount, fInterestRate, fYears);
because you don't actually have variables called fLoanAmount, fInterestRate or fYears.

Is that clear?
Sep 18 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.