Help with output formatting

This is a discussion on Help with output formatting within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get my output to look like this: year 1 year 2 year 3 year 4 year 5 ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Help with output formatting

    I'm trying to get my output to look like this:
    year 1 year 2 year 3 year 4 year 5

    6%:
    7%: THE ACTUAL ACCOUNT BALANCE DATA SHOULD APPEAR
    8%: IN HERE NICELY FORMATTED
    9%:
    10%:
    11%:
    12%:

    My output is like this:
    year1 year2 year3 year4 year5 <---the y in year1 is actually aligned with the 1 below it
    6% 1060 1123 1190 1261 1336
    7% 1070 1144 1224 1309 1400
    8% 1080 1166 1259 1359 1467
    9% 1090 1188 1294 1410 1536
    10% 1100 1210 1331 1464 1610
    11% 1110 1232 1367 1517 1683
    12% 1120 1254 1404 1572 1760

    how do i get these numbers to xxxx.xx format AND align this stuff up? I've been toiling with this for a couple of days now and i still cant get it...here's my source code

    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    using namespace std;
    int main ()
    {

    int amt=1000;
    float interest=.06;

    cout << setw(12) << "year1 ";
    cout << setw(8) << "year2 ";
    cout << setw(8) << "year3 ";
    cout << setw(8) << "year4 ";
    cout << setw(8) << "year5 " << '\n';

    for (int t=0; t<7; t++)
    {
    cout << (interest * 100) << "%";
    for (int x=0;x<5; x++)
    {
    amt = amt+(amt * interest);
    cout << setw(8) << amt;
    }
    amt=1000;
    interest=interest+.01;
    cout << endl;
    }
    system("pause");
    return 0;
    }
    Last edited by webvigator2k; 04-14-2003 at 10:04 PM.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    here you go:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    
    int main ()
    {
    
    	float amt=1000;
    	float interest=.06;
    
    	cout << setw(12) << "year1 ";
    	cout << setw(8) << "year2 ";
    	cout << setw(8) << "year3 ";
    	cout << setw(8) << "year4 ";
    	cout << setw(8) << "year5 " << '\n';
    	
    	for (int t=0; t<7; t++)
    	{
    		cout.setf (ios::fixed); 
    
    		cout << setw(4) << setprecision(0) << (interest * 100) << "%";
    		
    		for (int x=0; x<5; x++)
    		{
    
    			amt = amt+(amt * interest);
    			cout << setw(8) << setprecision(2) << amt;
    		}
    		
    		amt=1000;
    		interest=interest+.01;
    		cout << endl;
    	}
    	
    	system("pause");
    	return 0;
    }
    cout.setf (ios::fixed) will make sure that all floats will output with decimal points even if they are 0's

    setprecision tells the program to how many decimals to output

    One basic comment, in your original code you had atm as a int, which cannot hold decimal points..... so i changed it to a float.

    P.S. I know there is a way to incorporate the ios::fixed right into cout, but i forgot how to do it exactly and i do not rlly feel like looking it up so if you wish to it as such, look it up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 05:00 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 04:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 02:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 07:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21