class/struct question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrklai
    New Member
    • Jul 2010
    • 3

    #1

    class/struct question

    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.


    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    #include <limits>
    using namespace std;
    
    // End Headers
    
    double getLoanAmount();
    int getLoanChoice();
    
    void calculateMonthlyPayments(double loanAmount , double interestRate , double payment, int termInMonths);
    void printOutput(int month , double H , double C , double loanAmount);
    
    
    
    int main()
    {
    	double loanAmount ;
    	double interestRate[] = { .0535 , .055  , .0575 } ;	
    	int termInMonths[] = { 7*12  , 15*12 , 30*12 } ;	// in months
    		
    	int quit = 0;
    	do
    	{
    		loanAmount = getLoanAmount();
    		
    		int num = getLoanChoice();
    
    		// monthly payment calculation formula
    		double payment = loanAmount * (( interestRate[num-1]/12) /(1-pow( 1 / ( ( 1 + interestRate[num-1] / 12 ) ) , termInMonths[num-1] ) ) );
    		
    		// display the payment amount
    		cout << fixed << setprecision(2) <<"Monthly Payment:\t$" <<  payment << endl;
    		
    		// calculate and print monthly payments
    		calculateMonthlyPayments( loanAmount , interestRate[num-1] , payment, termInMonths[num-1] );
    	
    		cout << "\n1. Enter new data" << endl;
    		cout << "2. Quit" << endl;
    		cout << "Select a choice: ";
    		cin >> quit;
    	}
    	while ( quit != 2 );
    
    	return 0;
    }
    
    // input loan amount
    double getLoanAmount()
    {
    	double loanAmount;
    	// input loan amount
    	cout << "Enter Loan amount:" ;
    	cin >> loanAmount;
    	return loanAmount;
    }
    // input choice of user
    int getLoanChoice()
    {
    	int num;
    	do
    	{
    		cout << "1. 7 year at 5.35%\n2. 15 year at 5.5%\n3. 30 year at 5.75%\nSelect a mortgage loan: ";
    		cin >> num;
    	} while ( num < 1 || num > 3 );
    	return num;
    }
    // Calculate monthly payments of each installment
    void calculateMonthlyPayments(double loanAmount , double interestRate , double payment, int termInMonths)
    {
    	
    		// Heading of output
    		cout << "\nMonth\t\tMonthly\t\tPrinciple\t\tBalance" << endl;
    		cout << "\n\t\tInterest\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" << endl;
    		cout << "\n______________________________________________________________________" << endl;
    		double J = interestRate / 12 ;			 
    		double H = 0 , C = 0, Q  = 1 ;		
    		int month = 1;
    		
    //		do 
    //		{		
    			double HSum = 0 , CSum = 0 ;	
    			for ( int i = 1 ; i <= termInMonths ; i++ )
    			{
    				// calculate current monthly interest
    				H = loanAmount * J ;
    				// loanAmount amount to be paid in that month
    				C = payment - H ;						
    				CSum = CSum + C ;
    				HSum = HSum + H ;
    				// new balance of loanAmount of loan
    				loanAmount = loanAmount - C ; 	
    					
    				if ( loanAmount > 0 )
    				{
    					// display output
    					printOutput(month++ , H , C , loanAmount);
    					
    				}
    				// if 12 lines has been displayed then pause
    				if ( month % 12 - 1 == 0 )
    				{
    					// pause
    					cout << "Press Enter to continue viewing table." << endl;
    					std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    					//cin.get();
    				}
    			}			
    			Q = loanAmount;
    			
    			// set loanAmount to Q for next loop iteration
    			loanAmount = Q;
    
    [I][B][U]					}
    						while ( loanAmount >= 0 );
    						loanAmount = 0;
    						printOutput(month++ , H , C , loanAmount);
    
    					}
    [/U][/B][/I]		
    // display the monthly payment
    void printOutput(int month , double H , double C , double loanAmount)
    {
    	cout << month << "\t\t$" << fixed<< setprecision(2) << H << "\t\t$" << C << "\t\t$" << loanAmount << endl;
    }
    
    void printInformation(double loanAmount , double interestRate , double termInMonths, double payment)
    
    
    {
    
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It looks like a screwed-up whiuole loop.

    This code:
    Code:
            loanAmount = Q; 
    
                        } 
                            while ( loanAmount >= 0 ); 
                            loanAmount = 0; 
                            printOutput(month++ , H , C , loanAmount); 
      
                        }
    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:
    Code:
     loanAmount = Q; 
      
    			while ( loanAmount >= 0 )
    			{ 
                                                    loanAmount = 0; 
                            printOutput(month++ , H , C , loanAmount); 
      
                        }
    instead.

    Comment

    Working...