I am trying to make it so that no matter the length of the variable (eg char customer
with max length of 16
) will be aligned with the heading Customer
as so:
# Customer Pizza Price Time
---------+---------+---------+---------+---------+---------+---------+---------+
01 Nick Hawaiian $15.99 15
---------+---------+---------+---------+---------+---------+---------+---------+
Note: i am trying to implement this in a way where the variable of name is inputed by the user, thus its length differing.
Below i am getting a segmentation fault and in my code below and do not know why:
void print_header(struct pizzeria *the_pizzeria) {
printf("# Customer Pizza Price Time\n");
printf("--------------------------------------------------------------------------------\n");
}
void print_order(struct order *the_order, int order_number, bool selected) {
int space_pizza = (26 - (strlen(the_order->customer)));
int space_cost = (26 - (strlen(the_order->pizza)));
int space_time = 5;
if (selected == true){
printf("%02d", order_number);
printf(">%4s", the_order->customer);
printf("%*s", space_pizza, the_order->pizza);
printf("$%*0.2f", space_cost, the_order->cost);
printf("%*20s\n", space_time, the_order->time);
}
else{
printf("%02d", order_number);
printf(" %4s", the_order->customer);
printf("%*s", space_pizza, the_order->pizza);
printf("$%*0.2f", space_cost, the_order->cost);
printf("%*20s\n", space_time, the_order->time);
}
}
I tried to follow Left-pad printf with spaces of the answer by Rece Foc and edit by joe pelletier.
int space = 40;
printf("%*s", space, "Hello");
printf("%*d", space, 10);
printf("%*c", space, 'x');
Before I tried this method I did this:
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 am unsure what I am doing wrong here as I am unfamiliar with c and this way of printing values.
Here's a prototype that you can examine and 'tweak' to the column widths and using the data sources that you need.
The column widths should be easy to understand. The leading '-' on the '%s' definitions means "left justify in a field this wide".
Notice the trenary "? : " that sets the single character showing 'selected' to be either an empty SP or a GT symbol.
void print( bool selected ) {
int ordNo = 42;
char selchar = selected ? '>' : ' ';
char *name = "Bob the Builder";
char *pizza = "Meat Lovers";
double cost = 25.75;
char *time = "23:30:05"; // midnight munchies
char *fmt = "%-4d %c%-35s%25s%13.2f%10s\n";
printf( fmt, ordNo, selchar, name, pizza, cost, time );
}
int main() {
print( false );
print( true );
return 0;
}
Output:
42 Bob the Builder Meat Lovers 25.75 23:30:05
42 >Bob the Builder Meat Lovers 25.75 23:30:05
Make fine adjustments to the column widths, then test, then make more fine adjustments until you get what you want/need...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.