2 Replies - 865 Views - Last Post: 30 January 2011 - 06:08 PM Rate Topic: -----

#1 DanFurgason   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-January 11

I am having some trouble with an infinite loop using nested if's a

Posted 30 January 2011 - 05:12 PM

//This program reads the principal amount in an accout, the monthly interest rate and the amount withdrawn per month from a file.
//It then computes the number of years before the account is depleted.
//If the account will not deplete it displays a message informing the user.
//It continues until values of 0, 0, 0 are entered.

#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;

#define in_file "assignment1.txt"
#define out_file "resultassign1.txt"

int main()
{
	ifstream ins;
	ofstream outs;

	double principal, yearly_rate, monthly_rate, monthly_withdrawal, new_principal, number_months, N;
		
	ins.open(in_file);
	outs.open(out_file);
	
	ins >> principal >> yearly_rate >> monthly_withdrawal;
	monthly_rate=yearly_rate/12;

	while (principal !=0 || monthly_rate !=0 || monthly_withdrawal !=0)
	{
		N=0;
		new_principal=(principal*(1+monthly_rate))-monthly_withdrawal;
		if(new_principal>=principal)
			cout << "The account will never be depleted!" << endl;
		else
			new_principal=principal;
			number_months=N++;
			if(new_principal <=0)
				cout << "The number of years before the account is depleted is: " << N;
		ins >> principal >> yearly_rate >> monthly_withdrawal;
	}
	ins.close();
	outs.close();
	getch();
	return 0;
}


A sample input:
10000 0.06 500
0 0 0

Need help with the nested if's and incrementing variables.

This post has been edited by DanFurgason: 30 January 2011 - 05:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I am having some trouble with an infinite loop using nested if's a

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: I am having some trouble with an infinite loop using nested if's a

Posted 30 January 2011 - 05:22 PM

Your while loop is looping based on values of principal, monthly_rate and monthly_withdrawl. Where is all three every going to be 0? You never modify monthly_rate for instance. Did you perhaps mean to read into monthly_rate and not yearly_rate?

Also do realize that if you want more than one statement in a block, you have to enclose them in curly braces. For instance your else statement is only doing "new_principal = principal" it is always executing number_months = N++ because it is not part of the else.

:)

This post has been edited by Martyr2: 30 January 2011 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 DanFurgason   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-January 11

Re: I am having some trouble with an infinite loop using nested if's a

Posted 30 January 2011 - 06:08 PM

Okay, I think I'm missing something with my while statement still, here's what I have now:
	ins >> principal >> yearly_rate >> monthly_withdrawal;
	monthly_rate=yearly_rate/12;
	cout << "The principal amount is: " << principal << endl;
	cout << "The monthly rate is: " << monthly_rate << endl;
	cout << "The monthly withdrawal is: " << monthly_withdrawal << endl;
	while (principal !=0 && monthly_rate !=0 && monthly_withdrawal !=0)
	{
		N=0;
		new_principal=(principal*(1+monthly_rate))-monthly_withdrawal;
		if(new_principal>=principal)
			{
				cout << "The account will never be depleted!" << endl;
			}
		else
			{
				new_principal=principal;
				number_months=N++;
				if(new_principal <=0)
					{
						cout << "The number of years before the account is depleted is: " << N;
					}
			}
		ins >> principal >> yearly_rate >> monthly_withdrawal;
	}
	ins.close();
	outs.close();
	getch();
	return 0;
}



Here's the full input file:
10000 0.06 500
20000 0.07 300
15000 0.05 700
9000 0.03 1000
5000 0.02 300
8000 0.12 70
0 0 0

And this is what I'm getting during debugging:
The principal amount is: 10000
The monthly rate is: 0.005
The monthly withdrawal is : 500
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1