Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ Columns Aligment problem

  • 09-11-2014 10:28PM
    #1
    Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭


    Really cant get this to work, it's just 3 columns displayed by a for loop, first ones the number, second is that number squared and third is that number cubed.
    Cant get the third column to line up, I've tried
    << left ,
    <<right,
    with setfill() and without

    it coming out like this;

    5vUqwW.jpg

    Maybe I've these in the wrong place?
    for (int i = 0; i<=50; i+=5)
    	{
    		cout<< setw(10)<<"\n Integer =  "<<i;
    		cout<< setw(10)<<i<< char(253) << "  = "  <<i*i;
    		cout<< right <<setfill(' ') <<setw(10)<< i<< char(252) << "  = " <<i*i*i;
    

    Thanks in advance


Comments

  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,242 Mod ✭✭✭✭L.Jenkins


    Can you use the \t (tab) to evenly space the characters? So from the square numbers onwards to the cubed number, drop in a \t in quotes to see if it makes any difference.


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    Thanks for the suggestion, but no luck. It just prints out the same. I also can't work out why the middle row is lined up correctly when I have nothing to tell it I want it aligned.
    I'm sure the answer is either <<right or <<setfill(' '), but can't work it out.


  • Closed Accounts Posts: 1,095 ✭✭✭solomafioso


    Might be a bit of hassle, but you could think of putting the data in a table?

    http://stackoverflow.com/questions/14765155/how-can-i-easily-format-my-data-table-in-c


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    You need another setw() for the i*i part - setw() only affects the width of the next element in the stream.


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    Thanks for your help, but no difference. I think I'm just too dumb to get this, which has been bugging the **** out of me all week, this is due tomorrow and this is the simple part, the rest of the project was a PITA.
    I could sleep easy if I could just get that last column in some sort of aliment.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,335 ✭✭✭padraig_f


    Had a look at this. Looks like setw() only sets the width of the next stream element. So you need to set it multiple times, one for each number of variable length. I did the following:
    #include <iostream>
    #include <iomanip> 
    
    using namespace std;
    
    int main()
    {
    	int numberLength = 6;
    	for (int i = 0; i<=50; i+=5)
    	{
    		cout<<"\n Integer = "<< left<< setw(numberLength) << i;
    		cout<< right<< setw(numberLength)<< i<< char(253) << " = " << left << setw(numberLength) <<i*i;
    		cout<< right<< setw(numberLength)<< i<< char(252) << " = " << left<< setw(numberLength) <<i*i*i;	
    	}
    
    	cout<< endl;
    
    	return 0;
    }
    

    and got:
     Integer = 0          0? = 0          0? = 0     
     Integer = 5          5? = 25         5? = 125   
     Integer = 10        10? = 100       10? = 1000  
     Integer = 15        15? = 225       15? = 3375  
     Integer = 20        20? = 400       20? = 8000  
     Integer = 25        25? = 625       25? = 15625 
     Integer = 30        30? = 900       30? = 27000 
     Integer = 35        35? = 1225      35? = 42875 
     Integer = 40        40? = 1600      40? = 64000 
     Integer = 45        45? = 2025      45? = 91125 
     Integer = 50        50? = 2500      50? = 125000
    

    (my PC doesn't print the exponent numbers).


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    Padraig, I could make sweet sweet love to you all night long:pac:


Advertisement