473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

class/struct question

3
To anyone who can help...I'm trying to implement class/structure into my code, I overlooked this as part of my assignment and don't know how to implement without changing the code too much(it took me forever to get this right)...also, the underlined bold section (lines 116-121) is a recommendation I received from a friend but when I tried to implement it it gives me errors (no declaration or storage class)...if you can help me, please do.


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <math.h>
  5. #include <limits>
  6. using namespace std;
  7.  
  8. // End Headers
  9.  
  10. double getLoanAmount();
  11. int getLoanChoice();
  12.  
  13. void calculateMonthlyPayments(double loanAmount , double interestRate , double payment, int termInMonths);
  14. void printOutput(int month , double H , double C , double loanAmount);
  15.  
  16.  
  17.  
  18. int main()
  19. {
  20.     double loanAmount ;
  21.     double interestRate[] = { .0535 , .055  , .0575 } ;    
  22.     int termInMonths[] = { 7*12  , 15*12 , 30*12 } ;    // in months
  23.  
  24.     int quit = 0;
  25.     do
  26.     {
  27.         loanAmount = getLoanAmount();
  28.  
  29.         int num = getLoanChoice();
  30.  
  31.         // monthly payment calculation formula
  32.         double payment = loanAmount * (( interestRate[num-1]/12) /(1-pow( 1 / ( ( 1 + interestRate[num-1] / 12 ) ) , termInMonths[num-1] ) ) );
  33.  
  34.         // display the payment amount
  35.         cout << fixed << setprecision(2) <<"Monthly Payment:\t$" <<  payment << endl;
  36.  
  37.         // calculate and print monthly payments
  38.         calculateMonthlyPayments( loanAmount , interestRate[num-1] , payment, termInMonths[num-1] );
  39.  
  40.         cout << "\n1. Enter new data" << endl;
  41.         cout << "2. Quit" << endl;
  42.         cout << "Select a choice: ";
  43.         cin >> quit;
  44.     }
  45.     while ( quit != 2 );
  46.  
  47.     return 0;
  48. }
  49.  
  50. // input loan amount
  51. double getLoanAmount()
  52. {
  53.     double loanAmount;
  54.     // input loan amount
  55.     cout << "Enter Loan amount:" ;
  56.     cin >> loanAmount;
  57.     return loanAmount;
  58. }
  59. // input choice of user
  60. int getLoanChoice()
  61. {
  62.     int num;
  63.     do
  64.     {
  65.         cout << "1. 7 year at 5.35%\n2. 15 year at 5.5%\n3. 30 year at 5.75%\nSelect a mortgage loan: ";
  66.         cin >> num;
  67.     } while ( num < 1 || num > 3 );
  68.     return num;
  69. }
  70. // Calculate monthly payments of each installment
  71. void calculateMonthlyPayments(double loanAmount , double interestRate , double payment, int termInMonths)
  72. {
  73.  
  74.         // Heading of output
  75.         cout << "\nMonth\t\tMonthly\t\tPrinciple\t\tBalance" << endl;
  76.         cout << "\n\t\tInterest\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" << endl;
  77.         cout << "\n______________________________________________________________________" << endl;
  78.         double J = interestRate / 12 ;             
  79.         double H = 0 , C = 0, Q  = 1 ;        
  80.         int month = 1;
  81.  
  82. //        do 
  83. //        {        
  84.             double HSum = 0 , CSum = 0 ;    
  85.             for ( int i = 1 ; i <= termInMonths ; i++ )
  86.             {
  87.                 // calculate current monthly interest
  88.                 H = loanAmount * J ;
  89.                 // loanAmount amount to be paid in that month
  90.                 C = payment - H ;                        
  91.                 CSum = CSum + C ;
  92.                 HSum = HSum + H ;
  93.                 // new balance of loanAmount of loan
  94.                 loanAmount = loanAmount - C ;     
  95.  
  96.                 if ( loanAmount > 0 )
  97.                 {
  98.                     // display output
  99.                     printOutput(month++ , H , C , loanAmount);
  100.  
  101.                 }
  102.                 // if 12 lines has been displayed then pause
  103.                 if ( month % 12 - 1 == 0 )
  104.                 {
  105.                     // pause
  106.                     cout << "Press Enter to continue viewing table." << endl;
  107.                     std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
  108.                     //cin.get();
  109.                 }
  110.             }            
  111.             Q = loanAmount;
  112.  
  113.             // set loanAmount to Q for next loop iteration
  114.             loanAmount = Q;
  115.  
  116.                     }
  117.                         while ( loanAmount >= 0 );
  118.                         loanAmount = 0;
  119.                         printOutput(month++ , H , C , loanAmount);
  120.  
  121.                     }
  122.         
  123. // display the monthly payment
  124. void printOutput(int month , double H , double C , double loanAmount)
  125. {
  126.     cout << month << "\t\t$" << fixed<< setprecision(2) << H << "\t\t$" << C << "\t\t$" << loanAmount << endl;
  127. }
  128.  
  129. void printInformation(double loanAmount , double interestRate , double termInMonths, double payment)
  130.  
  131.  
  132. {
  133.  
  134. }
  135.  
  136.  
  137.  
Jul 19 '10 #1
1 1551
weaknessforcats
9,208 Expert Mod 8TB
It looks like a screwed-up whiuole loop.

This code:
Expand|Select|Wrap|Line Numbers
  1.         loanAmount = Q; 
  2.  
  3.                     } 
  4.                         while ( loanAmount >= 0 ); 
  5.                         loanAmount = 0; 
  6.                         printOutput(month++ , H , C , loanAmount); 
  7.  
  8.                     } 
a) has a ; in the while statement so there is really no loop. loanAmount is not defined inside the current scope.
It goes out of scope due to the }.


It looks like you want:
Expand|Select|Wrap|Line Numbers
  1.  loanAmount = Q; 
  2.  
  3.             while ( loanAmount >= 0 )
  4.             { 
  5.                                                 loanAmount = 0; 
  6.                         printOutput(month++ , H , C , loanAmount); 
  7.  
  8.                     } 
instead.
Jul 20 '10 #2

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

Similar topics

31
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from...
4
by: Erik H. | last post by:
I have an ASPX page in which I am trying to bind a datagrid to a dataset pulled from Microsoft Access DB using code inline method. For some reason, the compiler is having a problem with 'using'....
6
by: SB | last post by:
I feel dumb to ask because I bet this is a simple question... Looking at the code below, can someone please explain why I get two different values in my Marshal.SizeOf calls (see the commented...
3
by: Erik H. | last post by:
I have an ASPX page in which I am trying to bind a datagrid to a dataset pulled from Microsoft Access DB using code inline method. For some reason, the compiler is having a problem with 'using'....
5
by: Lyle Avery | last post by:
Hello guys, Look at this in c++ file: class T { public: char c; char ca; };
1
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
2
by: cr55 | last post by:
I was wondering if anyone can help me with this programming code as i keep getting errors and am not sure how to fix them. The error code displayed now is error: C2228: left of '.rent' must have...
2
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
3
by: rajatamilarasu | last post by:
for(i=0 ; i<listNum ; i++) { checker.checkN1DisagreementSwitch(pRep_Active_SList.ChassisID, pRep_Active_SList.SlotID, pRep_Standby_SList.ChassisID, pRep_Standby_SList.SlotID); ...
1
by: =?Utf-8?B?Y2hyaXNiZW4=?= | last post by:
Hi, Assuming I have a class/struct with only following types float, int, string what is the most efficient way to pack it to a single byte array before I send it out. I tried to use...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.