[英]How can i fix an issue with padding in c?
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:我正在努力做到这一点,以便无论变量的长度(例如,最大长度为16
的char customer
)都将与标题Customer
对齐,如下所示:
# 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.我试图跟随Left-pad printf 和Rece Foc 的答案空格并由joepellier 编辑。
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.我不确定我在这里做错了什么,因为我不熟悉 c 和这种打印值的方式。
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". '%s' 定义中的前导 '-' 表示“在这么宽的字段中左对齐”。
Notice the trenary "? : " that sets the single character showing 'selected' to be either an empty SP or a GT symbol.请注意三元组“?:”,它将显示“已选择”的单个字符设置为空 SP 或 GT 符号。
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: 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...对列宽进行微调,然后进行测试,然后进行更多微调,直到获得您想要/需要的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.