Thread: right justifying??

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    right justifying??

    Hi im a student and i have an assignment to do, and i seem to be a bit stuck.

    I have a list of numbers which i have read from a file i need them in a list like so,

    25.00
    43.00
    15.00
    456.00

    but i am getting,

    25.00
    43.00
    25.00
    456.00

    I have used %4.2f to display the numbers.

    Please help, thank you Ganonshin. Bye.
    Last edited by ganonshin; 05-20-2002 at 08:29 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: right justifying??

    Originally posted by ganonshin
    Hi im a student and i have an assignment to do, and i seem to be a bit stuck.

    I have a list of numbers which i have read from a file i need them in a list like so,

    25.00
    43.00
    15.00
    456.00

    but i am getting,

    25.00
    43.00
    25.00
    456.00

    I have used %4.2f to display the numbers.

    Please help, thank you Ganonshin. Bye.

    Well from the looks of it, you're getting exactly what you want. You should learn how to use formatting tags here. That aside, try using the - instead.

    %-4.2

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    Hi again Quzah.

    I have just tried using the - sign, it didnt do anything to the way my figures are displayed.

    The figures should be displayed justified from the right, so there should be a straight line down the right hand side of the figures, which there isn't.

    Heeeelllllpppp!!!!!!

    Ganonshin.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Why don't you just use a bunch of tabs?
    The world is waiting. I must leave you now.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    Hi Shadow.

    If i use tabs i cannot put my figures where i want to, can i??

    Can you expand on your theory?

    Ganonshin.

  6. #6
    sodsm live
    Guest
    ganoshin

    To get the results you were originaly trying for, and also make what you were already using work like you expected, with those figures you need to use at least %6.2f. The number before the dot(width specifier)means how many total characters to print including the decimal. Adding a zero to the begining of the width specifier pads the numbers out with zeros instead of blanks - ie %07.2f will give you:

    0025.00
    0043.00
    0015.00
    0456.00

    There are other and probably better ways to do this but whatever.

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    It was a bit hackish, but:
    Code:
    #include <stdio.h>
    
    void right( char * text );
    
    int main()
    {
    	right(" 25.00\n");
    	right(" 43.00\n");
    	right(" 25.00\n");
    	right("456.00\n");
    	return 0;
    }
    
    void right( char * text )
    {
    	printf("\t\t\t\t\t\t\t\t\t%s", text);
    }
    The world is waiting. I must leave you now.

  8. #8
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I haven't tested this, but if you're using printf, can't you use the width argument combined with an align right?

    http://www.cplusplus.com/ref/cstdio/printf.html


    [edit]
    Thanks Shadow, proof you should always test it
    [/edit]
    Last edited by Azuth; 05-21-2002 at 02:28 AM.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  9. #9
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Do you have that link right?
    The world is waiting. I must leave you now.

  10. #10
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    The proof

    [code]
    int main(void)
    {
    char num1[]={"425.00"};
    char num2[]={"50.00"};

    float num3=13;
    double num4=15.2;


    printf ("Number 1 %10s \n",num1);
    printf ("Number 2 %10s \n",num2,10);
    printf ("Number 2 %10.2f \n",num3,10);
    printf ("Number 2 %10.2f \n",num4,10);

    return 0;
    }
    [code]
    Demonographic rhinology is not the only possible outcome, but why take the chance

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or even
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	float	f;
    
    	for (f = 1; f < 100002; f *= 10)
    	{
    		printf("Number is now %10.2f \n", f);
    	}
    
    	return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    Thanks to all that have responded, below is part of the output.

    This is what I want,
    /* 4 15 20 1 7.0 105.00 11.0 165.00 */
    /* 4 7 10 2 9.0 90.00 11.0 110.00 */

    This is what I get

    /* 4 15 20 1 7.0 105.00 11.0 165.00 */
    /* 4 7 10 2 9.0 90.00 11.0 110.00 */

    I have justified all the specifiers exept the ones that will only print out one number, it seems to work alright with the 15 as you can see, the problem is with the 105.00.

    Ive tried everything I can think of, please help, Ganonshin.
    Last edited by ganonshin; 05-21-2002 at 12:32 PM.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use code tags that way you won't loose the formating in your post.

    And try making your width wider in printf() eg
    >print("%10.2f\n", flt);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    Hammer i have used tags but the format still aint right strange i must be doing something wrong.

    Also i have used what you wrote, but basicly the justification i am using works for the 15 (in by previous thread), but not for the 105.00. It prints like

    105.00
    90.00

    instead of

    105.00
    X90.00 /* minus the X of course*/

    Is that a bit clearer??

    Help please anyone, cheers from Ganonshin.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post the code that is doing the printing. Code tags are done like in my signature....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. right justifying numbers
    By arya6000 in forum C Programming
    Replies: 1
    Last Post: 11-26-2007, 05:14 PM
  2. String Justifying
    By m0ntana in forum C Programming
    Replies: 11
    Last Post: 04-09-2007, 03:24 PM
  3. justifying text
    By a.baki in forum C Programming
    Replies: 10
    Last Post: 04-20-2006, 02:36 PM
  4. Justifying text with strings
    By edwardvgrant in forum C Programming
    Replies: 6
    Last Post: 04-24-2004, 08:16 AM
  5. Right Justifying output
    By alpha in forum C++ Programming
    Replies: 5
    Last Post: 10-10-2003, 04:56 PM