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

[c++] setw probs

Options
  • 18-12-2002 5:55pm
    #1
    Closed Accounts Posts: 1,325 ✭✭✭


    Hey gain,

    anyone know why this is happening....

    i read a object name from a class using
    classname.getName();

    I try to format the ouput, as follows
    cout << setw(10) << classname.getName();

    BUT
    it treats the classname.getName()
    as though it is one character rather than,
    lets say 5chars long.
    Therefore i get
    name(followed by 9 spaces)

    When what i want is the name followed by
    4spaces.

    Is this normal???
    Any ideas???

    Cheers,
    A.


Comments

  • Registered Users Posts: 1,186 ✭✭✭davej


    I'm a bit rusty with c++ but my immediate solution would be to use printf instead. This will give you complete control over how your output is formatted.

    davej


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    1. printf is evil. Evil I tell you.
      Unless you regularly have to maintain old code you should mentally stick a big Of Historical and C-Compatibility Interest Only warning over everything you know about printf.
    2. Normally setw(10) followed by a five-character string should result in 5 spaces, and then the string, rather than the other way around.
      If you neglected to set the alignment to left from the default right then this would result in your formatting being wrong, and you may be misinterpreting which object's spacing is which, leading you to misdiagnose the problem as being with setw.
      Try:

      cout << setiosflags(ios::left) << setw(10) << classname.getName();


  • Registered Users Posts: 1,186 ✭✭✭davej


    printf is evil. Evil I tell you

    Ok I guess this is because it isn't typesafe..
    Just out of interest then, what would you use in C++ when you want to do complex String formatting ?

    I'm more used to Java (and Perl scripting)

    davej


  • Closed Accounts Posts: 1,325 ✭✭✭b3t4


    sorry ppl,
    didnt explain my prob all that well

    thing was that when i outputted the name
    it was then followed by another name.
    i was getting something like:

    name somethingElse
    longerName somethingNew

    When what i wanted was to get something like:

    name somethingElse
    longerName somethingNew

    (above should read somethingNew directly under somethingElse)

    in case anyones interested.....
    1. set width = 10
    2. get the string length
    3. width -= length
    4. use setw(width) on somethingElse and somethingNew

    this will output it in the format i wanted.

    using "\t" will work, although it doesnt format the data exactly as may be wanted when there is more than one string after the name. (afaik)

    Thanks all,
    A.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Ok I guess this is because it isn't typesafe

    Not typesafe, low-level, unintuitive, not a natural fit with the rest of the language, hard for the optomiser to get it's teeth into.

    I would use stream manipulators as above. There is nothing printf can do that stream manipulators can't do in a clearer fashion.

    And unlike the many cases where a C convention is replaced by a higher-level C++ convention efficiency isn't a valid counter argument as stream manipulators tend to be very easily optomised.
    I'm more used to Java (and Perl scripting)
    Strange that a Java coder would take the non-OO approach:confused:


  • Advertisement
Advertisement