0

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("%02d >%4s %20s $%0.2f %20s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time); } else{ printf("%02d %4s %20s $%0.2f %20s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time); } } 

I have also tried using %20s 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(("%02d >%4s" + String.format("%20s", the_order->pizza) + "$%0.2f" + String.format("%20s\n", the_order->time)), order_number, the_order->customer the_order->cost); } else{ printf(("%02d >%4s" + String.format("%20s", the_order->pizza) + "$%0.2f" + String.format("%20s\n", the_order->time)), order_number, the_order->customer the_order->cost); } } 

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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("%02d >%s %11s $%0.2f %3s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time); } else{ printf("%02d >%s %11s $%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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.