Extra line in Output with While Loop

Do-While Loop and While Loop

Page 1 of 1

1 Replies - 2457 Views - Last Post: 03 October 2009 - 07:22 PM Rate Topic: -----

#1 Sunny04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 09

Extra line in Output with While Loop

Post icon  Posted 03 October 2009 - 06:21 PM

I've been playing around with this code and finally have the output working correctly, however, there is an extra line the output that is all zeros and I don't know how to get rid of it. I would think the loop would end at month 24 when the balance is 0, but it doesn't. I am frustrated and losing patience and a hint would be nice. Thank you!

Note: I've tried using an if statement with break as part of the while loop code, but that did not change the output.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int month = 1;
	double balance = 1000, interest, debt, payment;

	cout << "This program shows you how many months it will take\n";
	cout << "to pay off a $1,000 debt, when paying $50 a month and\n";
	cout << "with an interest rate of 18% per year or 1.5% per month.\n";
	cout << "No down payment was required to get the credit plan.\n";
	cout << "\n";
	cout << "\t\tMonth\tInterest Paid\tDebt Paid\tBalance \n";
	do
	{
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);

		payment = 50;
		interest = balance * 0.015;
		debt = payment - interest;
		balance = balance - (payment - (balance * 0.015));
		cout << "\t\t" << month << "\t $" << interest << "\t\t $" << debt << "\t\t $" << fabs (balance) << endl;
		month++;
	}   while (balance > 50);


	while (balance > 0)

	{
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);

		interest = balance * 0.015;
		payment = balance + interest;
		debt = payment - interest;
		balance = balance - (payment - (balance * 0.015));
		cout << "\t\t" << month << "\t $" << interest << "\t\t $" << debt << "\t\t $" << balance << endl;
		month++;
	}

	char letter;
	cout << "Enter letter to end:\n";
	cin >> letter;

	return 0;
}



The current output:
Month   Interest Paid   Debt Paid	   Balance

		 1		  15.00			   35.00			 965.00

		 2		  14.47			   35.52			 929.48

		 3		  13.94			   36.06			 893.42

		 4		  13.40			   36.60			 856.82

		 5		  12.85			   37.15			 819.67

		 6		  12.30			   37.70			 781.97

		 7		  11.73			   38.27			 743.70

		 8		  11.16			   38.84			 704.85

		 9		  10.57			   39.43			 665.42

		10		   9.98			   40.02			 625.40

		11		   9.38			   40.62			 584.79

		12		   8.77			   41.23			 543.56

		13		   8.15			   41.85			 501.71

		14		   7.53			   42.47			 459.24

		15		   6.89			   43.11			 416.13

		16		   6.24			   43.76			 372.37

		17		   5.59			   44.41			 327.95

		18		   4.92			   45.08			 282.87

		19		   4.24			   45.76			 237.11

		20		   3.56			   46.44			 190.67

		21		   2.86			   47.14			 143.53

		22		   2.15			   47.85			   95.68

		23		   1.44			   48.56			   47.12

		24		   0.71			   47.12				0.00

		25		   0.00				 0.00			   -0.00


This post has been edited by Sunny04: 03 October 2009 - 06:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Extra line in Output with While Loop

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Extra line in Output with While Loop

Posted 03 October 2009 - 07:22 PM

It looks to me as if you don't even need this while loop. At least it seems to print the output correctly when I do this:

//    while (balance > 0)
//
//    {
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);

        interest = balance * 0.015;
        payment = balance + interest;
        debt = payment - interest;
        balance = balance - (payment - (balance * 0.015));
        cout << "\t\t" << month << "\t $" << interest << "\t\t $" << debt << "\t\t $" << balance << endl;
        month++;
 //   }

This post has been edited by OliveOyl3471: 03 October 2009 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1