Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ Columns Aligment problem

  • 09-11-2014 9: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,323 ✭✭✭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