quick question on strings
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: quick question on strings

  1. #1
    Join Date
    Jul 2007
    Posts
    41

    quick question on strings

    Hi Everyone,

    Just wondering how can I set the field width during printing. For e.g. how do I print something like the following in C++

    printf("%-24s,%-24s %u\n", mystr, myint);

    Tx !!

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: quick question on strings

    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: quick question on strings

    Should be like

    Code:
    std::cout << std::left << std::setw(24) << mystr 
                   << ",  0x" 
                   << std::hex << std::right << std::setw(8) << std::setfill('0') << myint
                   << std::endl;

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: quick question on strings

    Quote Originally Posted by itsmeandnobodyelse View Post
    Should be like

    Code:
    std::cout << std::left << std::setw(24) << mystr 
                   << ",  0x" 
                   << std::hex << std::right << std::setw(8) << std::setfill('0') << myint
                   << std::endl;
    don't forget the showbase flag/manipulator.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center