Thread: endless looping problem

  1. #1
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10

    endless looping problem

    Hi I'm sorry if this seem quite lame because for me it is as well
    I'm currently writing a program using Microsoft Visual 6 C++
    my program is as such
    Code:
    //This program is used to loop a number untill the value is found.
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void main()
    {
    	float value;
    	value = 1000;
    
    	int number;
    	number = 0;
    
    	float interest;
    	cout.setf(ios::fixed);
    	cout.precision(2);
    
    	float totalInterest;
    	totalInterest = 0;
    
    	float amount;
    	amount = 50;
    
    	while (value > 0)
    	{
    		interest = value *  0.15;
    		amount -= interest;
    		value -= amount;
    		totalInterest = totalInterest + interest;
    		number++;
    	
    
    	cout<<"The amount of times this process is repeated is "<<number<<endl;
    	cout<<"The total amount of the interest is "<<totalInterest<<endl;
    	cout<<"The amount left is "<<value<<endl;
    	}
    }
    this program of mine will loop endlessly if the value is 1000, but if the value is 100 it will provide me with the proper answer please help me out.

    P.S. I havent sleep in days thanks to this problem please help

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    	while (1)
    	{
    		interest = value *  0.15;
    		amount -= interest;
    		value -= amount;
    		totalInterest = totalInterest + interest;
    		number++;
    	
              if (value == 100) {
                break;
              }
              else {
     	    cout<<"The amount of times this process is repeated is "<<number<<endl;
    	    cout<<"The total amount of the interest is "<<totalInterest<<endl;
    	    cout<<"The amount left is "<<value<<endl;
              }
    	}

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So 15% of 1000 is 150, which means $50 a go won't even pay off the interest on the loan, let alone pay down the debt. Perhaps you intend to compute the payment value in some way.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10
    no, the way im trying this problem is like this to create a program which will calculate the amount of interest i need if i have different values (such as $100 or $1000 or even $1 million) with in mind my own budget which is $50.
    the interest program im going through is in a month ill be paying the interest as well as the debt with the $50. the interest is the debt's (value) 1.5% which = 0.15.

    If there is any way in solving it please tell me.
    And thanks very much to those that are willing to give me a hand.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10
    after asking some of my colleagues, i found out that i needed to tell the program to read the value in 2 decimal places. So after doing some manual calculation i discovered that the looping process should not be more than 25-30 (and its possible for it to be less) so if thats the way please tell me how to do it.
    ive tried the cout.precision but as i know that is only to display the value in the desired decimal places.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SoulMagician View Post
    no, the way im trying this problem is like this to create a program which will calculate the amount of interest i need if i have different values (such as $100 or $1000 or even $1 million) with in mind my own budget which is $50.
    the interest program im going through is in a month ill be paying the interest as well as the debt with the $50. the interest is the debt's (value) 1.5% which = 0.15.

    If there is any way in solving it please tell me.
    And thanks very much to those that are willing to give me a hand.
    1.5% is not 0.15. If you need to calculate the payment consisting of the interest owed plus 50 dollars then do so. (You'll be paying much more than $50 a month, though.)

  7. #7
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10
    Quote Originally Posted by tabstop View Post
    1.5% is not 0.15. If you need to calculate the payment consisting of the interest owed plus 50 dollars then do so. (You'll be paying much more than $50 a month, though.)
    hm im sorry, my mistake, after a some coffee and recalculation i discovered its actually 0.015. Which when multiplied to $1000 becomes only $15. Sorry for the confusion.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10
    after changing the program a bit, i still couldn't solve it

    heres the new program <Though it doesnt have much of a difference>

    Code:
    //This program is used to find the amount of time needed to compleete the payment of value
    //with the interest rate of 1.5% and where the user can only spare $50 to pay off 
    //everything. Meaning out of the $50, the user must pay the interest which is 1.5% of the debt
    //and the leftover of the $50 is used to pay off the debt.
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void main()
    {
    	float value;
    	value = 1000;
    
    	int number;
    	number = 0;
    
    	float interest;
    
    	float totalInterest;
    	totalInterest = 0;
    
    	float amount;
    	amount = 50;
    
    	while (value > 0)
    	{
    		interest = value *  0.015;
    		amount -= interest;
    		value -= amount;
    		totalInterest = totalInterest + interest;
    		number++;
    
    		if (value == 100)
    		{
    			break;
    		}
    		else
    		{
    	
    
    	cout<<"The amount of times this process is repeated is "<<number<<endl;
    	cout<<"The total amount of the interest is "<<totalInterest<<endl;
    	cout<<"The amount left is "<<value<<endl;
    		}
    		}
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to reset amount to $50 every time.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10
    You need to reset the amount to $50 every time
    sorry for the late reply, i was a bit busy with many stuffs this past few days. Anyways im sorry to say i don't exactly understand what do you mean tabstop

  11. #11
    Registered User
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    10
    thanks tabstop i finally understood
    THANK YOU VERY MUCH *bows*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  2. Endless Looping with const_iterator
    By CH_Tang in forum C++ Programming
    Replies: 4
    Last Post: 05-24-2008, 09:43 PM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM