Dumb alignment

This is a discussion on Dumb alignment within the C++ Programming forums, part of the General Programming Boards category; Really a silly question but this solution evades me. 1 + 1 = 2 10 + 1 = 11 2 ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    Dumb alignment

    Really a silly question but this solution evades me.

    1 + 1 = 2
    10 + 1 = 11
    2 + 2 = 4
    As you can see, double digits move the entire line one digit to the left, how can I correct this? I presume setw() is needed.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How do you want it to look?
    Code:
     1 + 1 = 2
    10 + 1 = 11
     2 + 2 = 4
    Or?
    Code:
     1 + 1 =  2
    10 + 1 = 11
     2 + 2 =  4

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Code:
     1 + 1 =  2
    10 + 1 = 11
     2 + 2 =  4

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    link to setw()
    Is the problem with using setw() or the largest number of digit not known?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    I can't get this sorted.

    Code:
    cout << setw( 7 ) << num1 << "  -  " << num2 << setw( 5 ) << "=  ";
    The above gives me the following:

    Code:
     4  -  3  =  x
    14  -  14  =  x
     6  -  2  =  x
    I make this change which still doesn't work:

    Code:
    cout << right << setw( 7 ) << num1 << "  -  " << num2 << setw( 5 ) << "=  ";
    Code:
    10  +  11  =  x
    6  +  1  =  x
    I'd like the subtraction and equal signs in a row in all cases with 1 to two digits but don't know how to get it like that. Help would be appreciated.

    Code:
    10  +  10 =  x
     9  +   1 =  x
     1  +  11 =  x
    Last edited by cyberCLoWn; 06-23-2004 at 02:07 PM. Reason: Slight modification..

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    try
    Code:
    cout<<setw(7)<<num1<<" - "<<setw(7)<<num2<<" = ";

  7. #7
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Got it.

    Code:
    cout << right << setw( 7 ) << num1 << "  -" << setw( 3 ) << num2 << setw( 5 ) << "=  ";

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    << setw( 5 ) << "=  ";
    Why? You are using a constent for setw's parameter on a fixed length string....

  9. #9
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Dunno, but it works

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    so would
    Code:
    <<"   = ";

  11. #11
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Guess I see what you mean. This was just a quick refresher project to get me back into the swing of C++. Have not really had a chance to touch it in a while.

    Thanks anyhow Thantos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic struct alignment?
    By matthew180 in forum C Programming
    Replies: 7
    Last Post: 06-15-2007, 09:34 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Ok, I'm dumb, byte alignment for the third time
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2002, 06:19 PM
  4. trouble with printf alignment
    By hyaline in forum C Programming
    Replies: 5
    Last Post: 09-22-2001, 02:39 AM

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