Thread: C Widths help

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    C Widths help

    Ok, im pretty new but am in a fundamentals course. I need help with widths.


    So, basically i need to read a int from the user and display it in a column...so something like this.

    int gallons;
    int gallons1;
    int gallons2;

    printf( "Enter this quarters usage: " );
    scanf( "%d", gallons );
    printf( "Enter last quarters usage: " );
    scanf( "%d", gallons1 );
    printf( etc etc
    etc etc
    etc etc


    what i am looking for, is to be able to take the input and set it say. 15 spaces from the left boarder...its a DOS program, fundamental class. Im not sure how this is suppose to be done, and help is greatly appreciated.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Simplest way is to print 15 spaces.

    REad up on scanf() - yours are not coded properly.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Oh? Are you speaking of me leaving out the '&'? I am aware, i was just submitting in a hurry.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes. OK.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    ? im not sure what that did. If i have input from a user, and i want to display it at lets say, 20 spaces from the left right justified. how is this done? Ive only seend this done in for loops and such.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The field width

    An optional decimal digit string (with non-zero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may write '*' or '*m$' (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int. A negative field width is taken as a '-' flag followed by a positive field width. In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.
    Learn to read doc...

Popular pages Recent additions subscribe to a feed