Text formatting

This is a discussion on Text formatting within the C++ Programming forums, part of the General Programming Boards category; Apparently this, and some other combinations, make the text appear like that: Code: cout << left << setw(20) << strc.name1 ...

  1. #1
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59

    Text formatting

    Apparently this, and some other combinations, make the text appear like that:

    Code:
    cout << left << setw(20) << strc.name1 << ' ' << strc.name2; cout << right << strc.total4 << endl;
    Code:
    NAME                    SURNAME20
    While it should appear like that:

    Code:
    NAME SURNAME        20
    Tried many combinations, seems like I couldn't hit the jackpot. Couldn't find any references focused on a situation like this (separation).

    ---

    An alternative to this would be printing whitespaces from an array (can't use strings).

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    It is doing exactly what you've told it to do.

    The `setw' mechanic does not stick; it is limited to the next insertion.

    The correct way to do this is with counting. You count the length of both outputs so that you can figure out how many characters to fill (via `setw') and optionally correct for lengths that extend beyond the relevant column.

    It is easier however to write those two bits of output to a temporary string stream and write the result at once.

    Soma

  3. #3
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    How can I do that? Or perhaps there is another way to make it work like it should? Note that I must use arrays.

    I can't use strings because I must use arrays. I'm getting ready for an exam, and we've this dumb system around here...

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    If you know enough about using character arrays to have written the code that manipulates `strc.name1' you know enough to do the counting I've suggested.

    If you didn't write that code, I suggest that you are in deep trouble.

    I'll be happy to help, but I'm not going to do it for you. I don't think anyone else here will do it for you.

    Try to do what I've suggested after a fashion. For example, do you know how to get the lengths of an input character array?

    Soma

  5. #5
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Well I've made it work now, but I think that there is an easier way.

    I know only one way - put a symbol that means "the array ends HERE," I'm not aware of a convenient way that strings offer to know the length of an array.

    Here is my for loop that does it, which I think is overly complicated. As, often, most of my code:

    Code:
    		for(int o=0;o<sizeof(strc.name2);o++)
    		{
    			if(strc.name1[o] == '0') strc.dummy++;
    		}

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    If you don't know how "C strings" (which are simply character arrays following a specific convention) work and `strc.name1' is an array you didn't write the code manipulating it correctly.

    Do you want to revise any of your statements?

    Or do you want to post the other bits of your code?

    By the way, your counting illustration is wrong; if that code is supposed to show how to count until a terminator is found your condition is wrong.

    Soma

  7. #7
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    My code works and I'm looking to improve my coding skills regarding this area, as the time is limited during exam. Overly-complicated code can take too long, so I'd rather post some code for revision instead.

    As for the counting illustration, I fill the array with memset(strc.name1,'0',20); to "clear" it, so basically looks for any fields that aren't "cleared."

    Here's some of my code regarding reading information from file:

    Code:
    // I read the name and the surname to two arrays
    // Since they are in the same line, separated by a whitespace
    ifile >> strc.name1;
    ifile >> strc.name2;
    I think that's a correct way to do it, as whitespaces can't (or shouldn't) be read, when using arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with formatting text
    By C89 in forum C Programming
    Replies: 10
    Last Post: 03-15-2012, 11:54 PM
  2. Help formatting text in c
    By anonymoususer in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 01:30 PM
  3. Text Formatting Program Help
    By Larph in forum C Programming
    Replies: 1
    Last Post: 05-02-2011, 08:39 AM
  4. Text Formatting & various questions.
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2005, 08:27 AM
  5. need help with formatting text
    By Flikm in forum C++ Programming
    Replies: 1
    Last Post: 09-06-2001, 07:52 PM

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