Thread: Noob alignment question

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    19

    Noob alignment question

    How can I fix my alignment?

    "%15.2f" would show a value aligned to the right of 15 spaces, how can I make it have a $ sign?

    I tried putting a $ sign after the conversion specifier but obviously that's an error. Right now it just shows as :
    Code:
    :               $8.99
    instead of
    : $              8.99
    How can I go about fixing this in my "%15.2f" portion?

    Obviously %$15.2f does not work.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    How about $%15.2f?

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    compile error, unknown conversion type character '.' in format

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Post the code

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    If you can see, I'd like it to have $ lined up with output cash amount on the right. The way I have it right now, would make it the $ next to the colon without it being lined with the amount.

    Code:
    /*
     This program is designed to be a basic cash register that prompts for a valid item code and quantity and computes the subtotal, tax, and change back.
     Written by: Bernard Wu
     Date: Feb 3rd 2013
     ID: 103321178
    */
    #include <stdio.h>
    //function prototypes
    void PrintHeader();
    void PrintFooter();
    void PrintHeader()
    {
        printf("---------------------------------------------------------------------\n\n");        //This is where the header is shown to be called in the main$
        printf("                _-=== Windsor Market Farm Fresh ===-_\n\n");
        printf("Welcome!\n");
    }
    
    void PrintFooter()
    {
        printf("---------------------------------------------------------------------\n\n");
        printf("THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN\n\n");   // This is where the footer is shown to be called at the end of $
        printf("---------------------------------------------------------------------\n");
    }
    int main()
    {
        int code;                                       //use this variable to enter the product codes
        float quantity;                                 //keeps the quantity of purchase in a float for decimals
        float subTotal = 0;                             //keeps a running subtotal of the purchase
        float paidCash;                                 //keeps how much money was given by the customer to pay for goods
        float price;                                    //displays the price of each item individually
        int validCode;                                  //bool check if the product code entered is valid or not
        //Display Heading
        PrintHeader();
        //Main Program loop
        do
        {
            do
            {
                printf("Enter Item Code: ");                                                    //prompt for item code
                scanf("%d", &code);                                                             //retrieve input from keyboard
                validCode = 1;                                                                  //this resets the bool flag to 1 if an invalid code was entered
                                                                                                //Check if 5 digits, if not code is not valid
                if ((code < 10000 || code > 99999) && (code != 0 || code != 00)){
                    validCode = 0;
                }
    
                                                                                                //Check 1st digit, cannot be more than 6
                if (code / 10000 == 7 && (code != 0 || code != 00))
                {
                    validCode = 0;
                }
                if (code / 10000 == 8 && (code != 0 || code != 00))
                {
                    validCode = 0;
                }
                if (code / 10000 == 9 && (code != 0 || code != 00))
                {
                    validCode = 0;
                }
                                                                                                //Check 2nd digit, cannot be greater than 3, 4 or 5 depending on$
                if (code / 1000 % 10 > 3 && (code != 0 || code != 00) && (code / 10000 == 1 || (code / 10000 == 2 || code / 10000 == 3))) // 2nd dig must be > 3$
                {
                    validCode = 0;
                }
                if (code / 1000 % 10 > 4 && (code != 0 || code != 00) && (code / 10000 == 4)) // 2nd dig must be > 4 if 1st dig is a 4
                {
                    validCode = 0;
                }
                if (code / 1000 % 10 > 5 && (code != 0 || code != 00) && (code / 10000 == 5)) // 2nd dig must be > 5 if 1st dig is 5
    
                {
                    validCode = 0;
                }
                if (code / 10000 == 6 && code / 1000 % 10 > 0 && (code != 0 || code != 00))
                {
                    validCode = 0;
                }
                if (validCode == 0){                                                        //if code is invalid, it will loop back to the beginning and ask for$
                    printf("<INVALID CODE, PLEASE TRY AGAIN>\n");
                }
            }while (validCode != 1);                                                        // if code is valid, prompt for quantity
            if ((validCode == 1 && code != 0) || code != 00){
            printf("Enter Quantity: ");                                                     // prompt quantity
            scanf("%f", &quantity);                                                         // retrieve from keyboard
            }
            //
            //This section prints what item was purchased along with it's quantity and updated subtotals
            //Calculations and new variable delcarations are also done here
            //
            if (code / 10000 == 1 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, local vegetables, $%.2f/lb @ %.1f = $%.15.f\n", price, quantity, price * quantity );
    
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 1 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, local fruits, $%.2f/lb @ %.1f = $%19.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 1 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, imports, $%.2f/lb @ %.1f = $%24.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 2 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, local vegetables, $%.2f each @ %.1f = $%13.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 2 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, local fruits, $%.2f each @ %.1f = $%17.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 2 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Produce Bulk, imports, $%.2f each @ %.1f = $%22.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 3 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Floral, roses, $%.2f each @ %.1f = $%30.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 3 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Floral, bouquet, $%.2f each @ %.1f = $%28.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 3 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Floral, house plant, $%.2f each @ %.1f = $%24.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 4 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Dairy, milk, $%.2f each @ %.1f = $%32.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 4 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Dairy, eggs, $%.2f each @ %.1f = $%32.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 4 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Dairy, butter, $%.2f each @ %.1f = $%30.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
     
            }
            else if (code / 10000 == 4 && code / 1000 % 10 == 4){
                price = (float)(code % 1000) / 100;
                printf("\t > Dairy, yogurt, $%.2f each @ %.1f = $%30.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 1){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, beef, $%.2f/lb @ %.1f = $%34.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 2){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, chicken, $%.2f/lb @ %.1f = $%31.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 3){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, turkey, $%.2f/lb @ %.1f = $%32.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 4){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, pork, $%.2f/lb @ %.1f = $%34.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 5 && code / 1000 % 10 == 5){
                price = (float)(code % 1000) / 100;
                printf("\t > Meats, rabbit, $%.2f/lb @ %.1f = $%32.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
            else if (code / 10000 == 6 && code / 1000 % 10 == 0){
                price = (float)(code % 1000) / 100;
                printf("\t > Other, other item, $%.2f each @ %.1f = $%26.2f\n", price, quantity, price * quantity );
                subTotal = subTotal + price * quantity;
            }
        }while(code !=0);                                               //loop will be repeated until item code 0 is entered
        //print and calculation statments to determine subtotal, total, tax and change
        printf("---------------------------------------------------------------------\n");
        printf("\t> SUB-TOTAL\t $%.2f\n", subTotal);
        printf("\t> TAX@7%%\t $%.2f\n", subTotal * 0.07);
        printf("---------------------------------------------------------------------\n");
        printf("\t> TOTAL\t $%.2f\n", subTotal * 1.07);
        printf("---------------------------------------------------------------------\n\n");
        printf("Enter Cash Amount: ");                                  // prompts for how much money customer gave
        scanf("%f", &paidCash);                                         // retrieves input from keyboard
        printf("\t> CHANGE TO CUSTOMER DUE: $%.2f\n\n", paidCash - subTotal * 1.07);    // displays how much change customer owes (if any)
        //Display Footer
        PrintFooter();
        return 0;                                                       //ends program
    }

    SAMPLE OUTPUT
    Code:
    ---------------------------------------------------------------------- 
    _-=== Windsor Market Farm Fresh ===-_ 
    Welcome! 
    Enter Item code: 12199 
    Enter Quantity: 2.5 
    > Produce Bulk, local fruits, $1.99/lb @ 2.5 =                   $4.98 
    Enter Item code: 44099 
    Enter Quantity: 3 
    > Dairy, yogurt, $0.99 each @ 3 =                                $2.97 
    Enter Item code: 4444 
    <INVALID CODE, PLEASE TRY AGAIN> 
    Enter Item code: 00 
    ---------------------------------------------------------------------- 
    > SUB-TOTAL $7.95 
    > TAX@7% $0.56 
    ---------------------------------------------------------------------- 
    > TOTAL $8.51 
    ---------------------------------------------------------------------- 
    Enter CASH AMOUNT: 20 
    >CHANGE TO CUSTOMER DUE: $11.49 
    ---------------------------------------------------------------------- 
    THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN 
    ---------------------------------------------------------------------- 
    
    Last edited by Bernard Wu; 02-06-2013 at 04:34 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    $ make store
    gcc -Wall -Wunreachable-code -ggdb3 -std=c99 -pedantic  -lm -lpthread  store.c   -o store
    store.c: In function ‘main’:
    store.c:92: warning: unknown conversion type character ‘.’ in format
    store.c:92: warning: too many arguments for format
    Line 92 is this one:
    Code:
    printf("\t > Produce Bulk, local vegetables, $%.2f/lb @ %.1f = $%.15.f\n", price, quantity, price * quantity );
    You need to remove the second . in that last format specifier. A editor with decent syntax highlighting might have shown that in the same color as a regular string, instead of a separate color for format specifiers or special chars, which would help you spot such errors.

    If you can see, I'd like it to have $ lined up with output cash amount on the right. The way I have it right now, would make it the $ next to the colon without it being lined with the amount.
    I can't think of a way off hand, to do that with just a printf specifier. One option involves a temporary char array. You sprintf the '$' and the value to the temp array with no width specifier (e.g. "$%.2f"), then use the "%s" modifier, with a width specifier, to print that string.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Location
    UK
    Posts
    19
    Code:
    printf("$/t%f", varible);
    Last edited by 0x47617279; 02-06-2013 at 07:02 PM.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    That will not work. As some lines are longer than the others and will not align exactly.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by 0x47617279 View Post
    Code:
    printf("$/t%f", varible);
    I don't think that's correct:
    Code:
    > cat foo.c
    #include <stdio.h>
    
    
    int main(void)
    {
        double varible = 123.45;
        printf("$/t%f", varible);
        return 0;
    }
    > make foo
    gcc -Wall -O0 -Wunreachable-code -ggdb3 -std=c99 -pedantic  -lm -lpthread  foo.c   -o foo
    > ./foo
    $/t123.450000
    If you meant \t, the tab character, that's still not correct. The OP wants (to the best of my understanding) the number aligned on the right, taking up whatever space it needs, with the $ immediately to the left, i.e. no spaces between the $ and the amount. Your code would print a dollar sign, then print the amount aligned on the next tab stop (typically 8 characters). That has the potential for leaving spaces between the $ and the amount.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob question
    By mouse666666 in forum C Programming
    Replies: 0
    Last Post: 01-12-2011, 04:58 AM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob question?
    By wart101 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-21-2006, 09:41 PM
  4. noob question
    By unclebob in forum C++ Programming
    Replies: 9
    Last Post: 09-24-2006, 08:48 AM