How to stop code from infinite looping?

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

    How to stop code from infinite looping?

    I'm having a problem with infinite looping of my code. i am new to C++ and am writing a mortgage calculator for school assignment and my instructor is of no help. I ran across the faq/chap 15 on <iostream> and attempted to input the example within my code and receive nothing but error messages since...Can someone point out to me what I'm doing wrong PLEASE.

    Code:
    /*Header Section*/
    
    #include "stdafx.h"
    #include <iostream>
    #include <math.h>
    #include <limits>
    using namespace std;
    
    /*End Header Section*/
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	double LoanAmount ;
    	double InterestRate ;	
    	double TermInMonths ;
    	int i = 0 ;
    	int Years;
    	int quit = 0;
    	do
    	{
    		// input loan amount
    		while ((std::cout << "Enter Loan amount without decimals: ")
    			&& (!(std::cin >> i) || i < 1 || i > 999999999)); 
    	} {
    			std::cout << "That's not a number between 1 and 999999999; ";
    			std::cin.clear();
    			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    			
    		// input interest rate
    		cout << "Enter interest rate(%): (Format 5.70 for 5.70%) " ;
    		cin >> InterestRate;
    		// input mortgage term
    		cout << "Enter terms of mortgage in years: ";
    		cin >> Years;
    
    		InterestRate = InterestRate / 100 ;
    		TermInMonths = Years * 12 ;
    
    		// monthly payment calculation formula
    		double payment = LoanAmount * (( InterestRate/12)/(1-pow( 1 / ( ( 1 + InterestRate / 12 ) ) , TermInMonths ) ) );
    		
    		cout << "\nLoan Amount:\t$" << LoanAmount << endl;
    		cout << "Interest Rate:\t" << InterestRate*100 << "%" << endl;
    		cout << "Terms in Months:\t" << TermInMonths << endl;
    		// display the payment amount
    		cout << "Monthly Payment:\t$" <<  payment << endl;
    
    		cout << "\n1. Enter new data" << endl;
    		cout << "2. Quit" << endl;
    		cout << "Select a choice: ";
    		cin >> quit;
    	
    	while ( quit != 2 );
    
    	return 0;
    	
    	}
    the errors i'm receiving are as follows:

    ...er.cpp(25): error C2059: syntax error : '{'
    ...er.cpp(25): error C2143: syntax error : missing ';' before '{'
    ...er.cpp(59): fatal error C1075: end of file found before the left brace '{' at 'c:\users\charl es i. clay\desktop\pr g 410\er\er\er.cp p(13)' was matched
    1>
    1>Build FAILED.

    I am completely lost as i've gone over it a million times.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your do loop is oncorrectly coded:

    Code:
    do
    {
    
    }  while (test);
    Now look at yours.

    Comment

    • mrklai
      New Member
      • Jul 2010
      • 3

      #3
      Thank you for pointing that out...I guess I had been staring at it too long to see that.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        ...and simplify the (test) of the do.......while( ) loop [l23 - l24] so that it is more easily understood.

        Comment

        Working...