Thread: Adjoining Strings

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Adjoining Strings

    I would like to display strings and few spaces after each string in a menu form

    i.e
    Code:
    #   1. First Menu      #
    #   2. Second Menu     #
    etc...

    using..
    Code:
            int choice =1;
            string descr = "First Menu";
    
    	cout << "     \t\t#" << choice <<  "."  <<  descr << "                    #\n";
    But the spaces after description will vary which will place the last '#' on different position, or badly aligned...

    My solution to this is have two strings
    Code:
         string descr;
         string whiteSpace = "                 ";
    How do i join these two in such a way that descr overlaps (occupied the first part of ) whitespace?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you'd like to use setw() to indicate how wide you want the spacing to be? You can use setw() on a " " too...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You just need to calculate how many spaces to add, then create a string with the appropriate number of spaces. Adapting your solution:
    Code:
    const int max_descr_width = 16;
    string descr = "First Menu";
    string whiteSpace(max_descr_width - descr.length(), ' ');

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Perhaps you'd like to use setw() to indicate how wide you want the spacing to be? You can use setw() on a " " too...

    --
    Mats
    Yes but still the start position to display setw(x) X number of times depends on the lenght of the preceding string..
    i.e
    Code:
    cout  <<  "James" << set(5) << "     " <<  "%";
    cout  <<  "Michelangelo" << set(5) << "     " << "%" ;
    % will be displayed 5 spaces after James, 5 spaces after Michelangelo.....

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, so you have to do setw() on the first part of the line too:
    Code:
    cout  <<  setw(12) << "James" << set(5) << "     " <<  "%";
    cout  <<  setw(12) << "Michelangelo" << set(5) << "     " << "%" ;
    Of course, the solution suggested by Daved is also good.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Daved View Post
    You just need to calculate how many spaces to add, then create a string with the appropriate number of spaces. Adapting your solution:
    Code:
    const int max_descr_width = 16;
    string descr = "First Menu";
    string whiteSpace(max_descr_width - descr.length(), ' ');
    How do i append a character at the and of a string of type string? strncat() copies from strings, not character., and i think it works with char *

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The easiest way is to use +=.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Daved View Post
    The easiest way is to use +=.
    didn't know u can do that...thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM