Thread: Infinite Loop!!!

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Question Infinite Loop!!!

    First, I wanted to thank those of you who replied to my last message on or about 12/01 on a problem I was having with a program to tell the day of the week from any date. One of you figured out it was suppose to use the Zeller formula, which was right (I'm surprised you could tell, it was such a clumzy attempt!). Anyway, with your help I managed to figure out what I was doing wrong. Thank again!

    As I am just learning programming, so I am trying to digest a little at a time. This project is really stumping me! I have got it to compile, but it goes into an infinite loop. If I try a different type of loop (for, do. . .while, etc.) it gives a bush of errors.

    P.S. Oh, by the way, I am using Code Warrior. I'm not sure if that makes any difference, but just in case. Thanks again in advance.



    //Project 8 - Reads amount of a loan, annual interest rate, &
    //monthly payment. Then displays the payment number, interest for that
    //month, the balance remaining after that payment, & total internest
    //paid to date in a table with appropriate headings

    #include <iostream>
    #include <iomanip>
    #include <cmath>

    using namespace std; //introduces namespace std

    void LoanCalc(double, double, double);//prototype

    double main( double, double, double )
    {
    double a, b, c;
    char YorN;

    do
    {
    cout << "What is the loan amount?\n";
    cin >> a; //loan amount
    cout << "What is the is annnual interest rate as decimal?\n";
    cin >> b; //annual interest rate
    cout << "What is the amount of monthly payment?\n";
    cin >> c; //monthly payment

    LoanCalc(a, b, c);

    cout << "\n\nEnter Y to do another, N to stop.\n\t\t";
    cin >> YorN;
    }
    while (YorN=='Y'||YorN=='y');

    return 0;
    }
    void LoanCalc(double a, double b, double c)//function definition
    {
    cout << "\n\tPymt #\t\tMon's Int\tBalance\t\tTot Int Pd\n";
    cout << "\t==========================================\ n";

    cout << fixed << showpoint << right << setprecision(2);

    double Balance = a;
    int count = 0;

    for(;
    {
    count ++;
    double MonIntRate = b / 12;
    double MonInt = a * MonIntRate;
    double PrinPymt = c - MonInt;
    double TotIntPd = TotIntPd + MonInt;
    double Balance = Balance - PrinPymt;

    cout << count << "\t" << MonInt << "\t" << Balance << "\t" << TotIntPd << "\n";

    if(Balance <= c) cout << "\tLast Pymt\t" << Balance + (Balance * MonIntRate) << "\t"
    << (Balance + (Balance * MonIntRate)) + TotIntPd;
    else break;
    }


    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    you do know that you need parameters for your for loop, don't you?

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Unhappy Error to Infinite Loop!!!

    Should have been: Sorry!?!

    //Project 8 - pg380#16 Reads amount of a loan, annual interest rate, &
    //monthly payment. Then displays the payment number, interest for that
    //month, the balance remaining after that payment, & total internest
    //paid to date in a table with appropriate headings

    #include <iostream>
    #include <iomanip>
    #include <cmath>

    using namespace std; //introduces namespace std

    void LoanCalc(double, double, double);//prototype

    double main( double, double, double )
    {
    double a, b, c;
    char YorN;

    do
    {
    cout << "What is the loan amount?\n";
    cin >> a; //loan amount
    cout << "What is the is annnual interest rate as decimal?\n";
    cin >> b; //annual interest rate
    cout << "What is the amount of monthly payment?\n";
    cin >> c; //monthly payment

    LoanCalc(a, b, c);

    cout << "\n\nEnter Y to do another, N to stop.\n\t\t";
    cin >> YorN;
    }
    while (YorN=='Y'||YorN=='y');

    return 0;
    }
    void LoanCalc(double a, double b, double c)//function definition
    {
    cout << "\n\tPymt #\t\tMon's Int\tBalance\t\tTot Int Pd\n";
    cout << "\t==========================================\ n";

    cout << fixed << showpoint << right << setprecision(2);

    double Balance = a;
    int count = 0;

    for(;
    {
    count ++;
    double MonIntRate = b / 12;
    double MonInt = a * MonIntRate;
    double PrinPymt = c - MonInt;
    double TotIntPd = TotIntPd + MonInt;
    double Balance = Balance - PrinPymt;

    cout << count << "\t" << MonInt << "\t" << Balance << "\t" << TotIntPd << "\n";

    if(Balance <= c) cout << "\tLast Pymt\t" << Balance + (Balance * MonIntRate) << "\t"
    << (Balance + (Balance * MonIntRate)) + TotIntPd;
    else break;
    }


    }

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Question Why does it keep posting the same way?

    Why does my message keep posting with a smiley face right after the for with only one semi-colon and no ")" at the end. Is it trying to tell me something?

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    29

    Smilies

    Its probably converting ;) to the winking smiley.... check "Disable smilies in this post" to turn that feature off when you post.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I got your code to compile and run sensibly, (I didn't check the result, only that it didn't loop), by removing your declarations from inside your loop thus...

    Code:
    void LoanCalc(double a, double b, double c)//function definition 
    { 
    	double MonIntRate; 
    	double MonInt; 
    	double PrinPymt; 
    	double TotIntPd; 
    	cout << "\n\tPymt #\t\tMon's Int\tBalance\t\tTot Int Pd\n"; 
    	cout << "\t==========================================\n"; 
    	
    	cout << fixed << showpoint << right << setprecision(2); 
    	
    	double Balance = a; 
    	int count = 0; 
    	
    	for(;;) 
    	{ 
    		count ++; 
    		MonIntRate = b / 12; 
    		MonInt = a * MonIntRate; 
    		PrinPymt = c - MonInt; 
    		TotIntPd = TotIntPd + MonInt; 
    		Balance = Balance - PrinPymt; 
    		
    		cout << count << "\t" << MonInt << "\t" << Balance << "\t" << TotIntPd << "\n"; 
    		
    		if(Balance <= c) cout << "\tLast Pymt\t" << Balance + (Balance * MonIntRate) << "\t" 
    			<< (Balance + (Balance * MonIntRate)) + TotIntPd; 
    		else break; 
    	} 	
    }
    ... I never declare variables the way you are, always at the top of the routine. C++ allows you to declare anywhere, just because it is legal, doesn't make it good style, and now as you've seen, can cause you all kinds of problems if you're not careful.

    As an aside, why are you declaring yopur main as float with three parameters and actually returning an integer without ever passing any parameters to main() ???
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Question Infinite Loop?

    So, you're saying I should always declare my variables before loop or I could get strange results, like an "infinite loop"? lol!!
    I did not quite understand you're question at end? I thought I needed to put "void" in prototype and declaration of function because the function doesn't return a value, just "cout", right? But then, what do I put as type for "main"?

  8. #8
    Unregistered
    Guest
    doesn't matter where or how you declare your variables, that doesn't cause an infinite loop. Hes saying declare your variables globally? so that its easier and neater to read and process. He also said that you are passing float values to main, but you dont even return values of type float. Secondly i suppose you are suppose to be sending values to main and you dont.

  9. #9
    Unregistered
    Guest
    scratch that anywhere line

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    catmom:

    This is your main() function...

    double main( double, double, double )
    {
    double a, b, c;
    char YorN;

    do
    {
    cout << "What is the loan amount?\n";
    cin >> a; //loan amount
    cout << "What is the is annnual interest rate as decimal?\n";
    cin >> b; //annual interest rate
    cout << "What is the amount of monthly payment?\n";
    cin >> c; //monthly payment

    LoanCalc(a, b, c);

    cout << "\n\nEnter Y to do another, N to stop.\n\t\t";
    cin >> YorN;
    }
    while (YorN=='Y'||YorN=='y');

    return 0;
    }

    ... if you declare your main() as double, when you return at the end, you should return a double, (heaven knows why you would want to but.!..), main() is like other functions, declare it as returning something, and then return that something, or declare it void and return nothing. Correct...

    int main()
    {
    return 0;
    }

    ... possible but very poor ...

    void main()
    {
    return;
    }

    ... in general, your operating system likes to have a completion status for the task when it exits, I don't think any demand it nowdays, but it should be done.

    As for your loop, try it the way you did it - infinite loop, try it the way I did it, no infinite loop. You do not need to declare the variables as global, they only need to be declared withing the scope of the function that uses them. Redeclaring them is horrible. I have not thought the whole thing through as I don't have time right now but consider this line...

    double TotIntPd = TotIntPd + MonInt;

    ... you would appear to be initialising a variable with its uninitialised self plus something else! Whatever, it looked horrid to me and I moved your declarations, then no more infinity...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM