fstream and formatting

This is a discussion on fstream and formatting within the C++ Programming forums, part of the General Programming Boards category; Hello I have a question...we are in spring break at school so I can't get in touch with my teacher. ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    21

    Unhappy fstream and formatting

    Hello I have a question...we are in spring break at school so I can't get in touch with my teacher. Can some kind soul explain what functions I may use to format my program so when it prints out it is all in line...notes from classmate say use library functions??
    ex
    john doe 111-45-6723
    jane doe 234-55-1212

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    you are going to have to do a better job explaining what you want...

    ofsteam fin;
    fin<< "John Doe 999-99-9999";

    prints everying in a line to a file

    cout << "blah blah blah";

    prints everything in a line to standard output (monitor).

    What exactly are you trying to do? What OS? What compiler? Or any relevant information at all would be nice...
    Blue

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    21
    sorry i didnt give relevant info i dont want people to think that i want them to do my work for me....what i need to do is read in info from a file. the file states the persons first and last name, age, and ss# in a line.....what i need to do is read that info in and then format it so everything lines up
    ex( i read in)
    john doe 18 123-45-6789
    jane doe 21 234-56-7890
    carrie doe 111 456-78-9012

    once i out put that info i need to have the info displayed to the screen in a line
    all first names line up
    all last names line up
    all ages line up
    all ss#'s line up

    my os is microsoft windows and my compiler is visual c++
    does that help????=)

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You need to look up setw(), in <iomanip>

    os << setw(10) << name << " " << setw(2) << age; //etc

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    shouldn't carrie doe be carrie allota doe

    sorry bad pun...

    The problem with setw() is that it reads the characters from the end of output and lines up the characters based on what is left over... so

    cout << setw(5) << "joe";

    would put two spaces and the 'joe'.

    but

    cout << setw(5) << "joel";

    would only put one space and then 'joel'.

    The best way I can think to do it would be to get the size of the string (char array, whatever) that you have and subtract that from a number that can represent available spaces after the string, then use a while loop to output those spaces. Then place your next string, subtract spaces, while loop remaining spaces, place next string. Make sense?

    so lets say you want names to be no longer than 20 characters, then you want 'at least' five spaces. The number you would have is 25.

    Joe has three letters, so you would output 22 spaces after joe and place the age.

    Well ages won't be more than three characters, and you would want 'at least' 5 spaces... so the number would be eight.

    An age of 97 would give you six spaces after whereas an age of 100 would give you five spaces.

    Then you put the social security number, newline, and do the process again.
    Last edited by Betazep; 03-04-2002 at 08:36 PM.
    Blue

Popular pages Recent additions subscribe to a feed

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