Home > Software design >  How to format outputs from printf in c
How to format outputs from printf in c

Time:08-27

Im new to C and trying to format a string that correlates to the spacing of another.

I am aiming for the example output below:

Orders for Pizzeria Freddy's
#   Customer                  Pizza               Price     Time      
------------------------------------------------------------------------
01 >Fred                      Hawaiian            $15.99    15

To do this i made two functions:

void print_header(struct pizzeria *the_pizzeria) {
    printf("Orders for Pizzeria %s\n", the_pizzeria->name);
    printf("#   Customer                  Pizza               Price     Time\n");
    printf("------------------------------------------------------------------------\n");

}

and

void print_order(struct order *the_order, int order_number, bool selected) {

    if (selected == true){
        printf("d >%4s  s $%0.2f  s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time);
    }

    else{
        printf("d  %4s  s $%0.2f  s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time);
    }

}

I have also tried using s to format the string in function print_order but got errors and not the intended output:

void print_order(struct order *the_order, int order_number, bool selected) {
    if (selected == true){
        printf(("d >%4s"   String.format(" s", the_order->pizza)   "$%0.2f"   String.format(" s\n", the_order->time)), order_number, the_order->customer the_order->cost);
    }

    else{
        printf(("d >%4s"   String.format(" s", the_order->pizza)   "$%0.2f"   String.format(" s\n", the_order->time)), order_number, the_order->customer the_order->cost);
    }

}

CodePudding user response:

It will add spaces at the end to

int printPad(int len, const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    int plen = vprintf(fmt, args);
    for(unsigned pad = 0; pad < len - plen; pad   ) putc(' ', stdout);
    va_end(args);
    return len;
}

and print field by field (one field at each call)

CodePudding user response:

despite not mentioning a lot of information like your main code or the datatypes inside struct order and struct pizzeria , but also there are many things to keep in mind when talking about C , there is no bool data type in C , instead it's a type defined value in header called #include<stdbool.h> , but it's not a primitive datatype in C , also values like true and false are hash defined values in same header to values 1 and 0 respectively , for the problem of padding , I liked the above answer made by 0___________ , but the solution I posted below is just using trail and error to find perfect padding suitable for you , also I added the missing information code blocks like main, etc , here is my solution:

#include <stdio.h>
#include <stdint.h>

typedef uint8_t bool;
#define true    1
#define false   0

struct pizzeria{
    char *name;
};

struct order{
    char *customer;
    char *pizza;
    float cost;
    char *time;
};

void print_header(struct pizzeria *the_pizzeria) {
    printf("Orders for Pizzeria %s\n", the_pizzeria->name);
    printf("#   Customer    Pizza       Price   Time\n");
    printf("------------------------------------------------------------------------\n"); 
 

}

void print_order(struct order *the_order, int order_number, bool selected) {

    if (selected == true){
        printf("d >%s s    $%0.2f %3s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time);
    }

    else{
       printf("d >%s s    $%0.2f %3s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time);
    }

}

int main(){
    struct pizzeria Freddy = {"Freddy's"};
    struct order theOrder = {
            .cost = 15.99f,
            .time = "15",
            .customer = "Freddy's",
            .pizza = "Hawaiian"
    };

    print_header(&Freddy);
    print_order(&theOrder, 1, true);

    return 0;
}
  • Related