Lining up columns nicely.


Posted on 16th Feb 2014 07:03 pm by admin

Hello everyone, I'm having problems with making columns line up properly. Here is my code:




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include #include using namespace std; int main () { double price = 1; //stores the starting price double yearInt = 1; //stores the yearly interest double monthInt; //will store monthly interest once yearly interest is added double payment = 1; //stores how much is paid per month double month = 1; //month counter double paidInt; //displays how much interest was paid double paidPrice; //displays how much of loan was paid off after interest double balance ; //displays balance after payment is made double remBalance; { cout << "Please enter the dollar amount of the loan: $"; cin >> price; cout << "Please enter the yearly rate of interest: "; cin >> yearInt; cout << "Please enter the monthly payment made: $"; cin >> payment; cout << "Month Principle Interest Paid Principle Paid Remaining Balance" < 0) { month++; paidInt = balance * monthInt; paidPrice = payment - paidInt; remBalance = balance - paidPrice; cout << month << setw(6) << " "<< balance << setw(6)<< " "<< paidInt << setw(6) << " "<< paidPrice << setw(6) << " "<< remBalance << endl; balance = remBalance; } cout << "Number of months to pay off loan: " << month << endl; cout << "Total interest paid on loan: " << endl; cout << "You have a credit of: " << remBalance << endl; } }



At the moment I'm using the setw manipulator. When the program prints, it looks like this:



Please enter the dollar amount of the loan: $400 Please enter the yearly rate of interest: 20 Please enter the monthly payment made: $20 Month Principle Interest Paid Principle Paid Remaining Balance 2.00 400.00 6.67 13.33 386.67 3.00 386.67 6.44 13.56 373.11 4.00 373.11 6.22 13.78 359.33 5.00 359.33 5.99 14.01 345.32 6.00 345.32 5.76 14.24 331.07 7.00 331.07 5.52 14.48 316.59 8.00 316.59 5.28 14.72 301.87 9.00 301.87 5.03 14.97 286.90 10.00 286.90 4.78 15.22 271.68 11.00 271.68 4.53 15.47 256.21 12.00 256.21 4.27 15.73 240.48 13.00 240.48 4.01 15.99 224.49 14.00 224.49 3.74 16.26 208.23 15.00 208.23 3.47 16.53 191.70 16.00 191.70 3.19 16.81 174.89 17.00 174.89 2.91 17.09 157.81 18.00 157.81 2.63 17.37 140.44 19.00 140.44 2.34 17.66 122.78 20.00 122.78 2.05 17.95 104.83 21.00 104.83 1.75 18.25 86.57 22.00 86.57 1.44 18.56 68.02 23.00 68.02 1.13 18.87 49.15 24.00 49.15 0.82 19.18 29.97 25.00 29.97 0.50 19.50 10.47 26.00 10.47 0.17 19.83 -9.36 Number of months to pay off loan: 26.00 Total interest paid on loan: You have a credit of: -9.36 Press any key to continue . . .

I am aware I can just change setw to a higher number to make it look nicer, but I want to do more than that. If you look at where the 10th month is, the whole line is indented a little bit and doesnt line up properly with the above lines. I am also concerned that if the user enters numbers with more digits, the output won't line up properly either.

How can I make it so that everything will line up nicely, no matter what (within reason) the user enters?

Thanks a lot for any help.

No comments posted yet

Your Answer:

Login to answer
218 Like 42 Dislike
Previous forums Next forums
Other forums

Am I setting up my sites oldschool?
Ok,

When I create a site I open a basic template that I have created that consists of a css f

Hashing?
Hi, I was hoping to take a string value then convert it - consistently - across each of our pages in

How do I get the row number from from an sql table query
Hi all

I have a table that I query and it returns a number of rows.
mysql_num_rows($query)

Need some ideas as to how to go about sorting this array...
Here's how the array's are created..

Code: <?php
while ($x = readdir($dp)) {

Help with ORDER BY
Hello. I would like to order by ascending States, then Cities, then Gyms in the following code, but

SWF image using php?
Hello once again.

My latest en devour requires me to produce an image of a static .swf that i

php mysql query from input textbox
Hi,

I have a text box, in that i have given the mysql query. I can able to get the query in v

problems with search form numerical "between" sending by php
I am trying to display the results of a search, on a sql database, on a web page. I set up html &quo

parse error
Parse error: syntax error, unexpected '[', expecting ')' in /Users/admin/Sites/phptest/array.php on

Do not allow posting of whitespace
Currently the script below works if the user does not type a name/message, but if i create a whitesp

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash