gpt4 book ai didi

c - 为什么C跳到下一个printf而不是等待scanf?

转载 作者:行者123 更新时间:2023-11-30 20:03:28 24 4
gpt4 key购买 nike

首先,这是我的代码:

typedef struct Customer {
char name[50];
char billing_address[100];
char phone_number[15];
double amount_paid;
double amount_due;
char date[20];
} Customer;

Customer customer;

printf("CREATE A CUSTOMER PROFILE\n=========================\n");

printf("Name: ");
scanf("%s", customer.name);

printf("Billing Address: ");
scanf("%s", customer.billing_address);

printf("Phone Number: ");
scanf("%s", customer.phone_number);

printf("Amount Paid: ");
scanf("%lf", &customer.amount_paid);

printf("Amount Due: ");
scanf("%lf", &customer.amount_due);

printf("Payment Date: ");
scanf("%s", customer.date);

if (strlen(customer.name) == 0 || strlen(customer.billing_address) == 0 || strlen(customer.phone_number) == 0 || strlen(customer.date) == 0) {
printf("All fields must be filled in!");
create_customer_profile(is_admin);
}

FILE *file = fopen("customers.txt", "ab+");

fprintf(file, "[%s], [%s], [%s], [%lf], [%lf], [%s]", customer.name, customer.billing_address, customer.phone_number, customer.amount_paid, customer.amount_due, customer.date);

printf("The record for the customer has been saved successfully!");

基本上,我在编写这段代码后正在测试我的应用程序。这是控制台的 View :

CREATE A CUSTOMER PROFILE
=========================
Name: Hassan
Billing Address: Jalan Barat
Phone Number: Amount Paid: 011111111
Amount Due: 200
Payment Date: "12/12/2000"
The record for the customer has been saved successfully!
Process finished with exit code 0

如上所示,系统不会等待“电话号码”,而是跳转到同一行上的“已支付金额”。这是我的文件输出:

[哈桑]、[惹兰]、[巴拉特]、[11111111.000000]、[200.000000]、["12/12/2000"]

出于某种原因,电话号码中填写了“Barat”,而不是地址中的“Jalan Barat”。我希望得到一些帮助来解释为什么 C 会这样。

更新:

使用此输入:

CREATE A CUSTOMER PROFILE
=========================
Name: hahah
Billing Address: lmfaooooooo
Phone Number: 01011010101001
Amount Paid: 010101
Amount Due: 393993
Payment Date: 24/12/2018
The record for the customer has been saved successfully!
Process finished with exit code 0

正如您在上面看到的,我没有在帐单地址字段中包含空格,因此,它可以与文件中的此输出配合使用:

[hahah], [lmfaooooooo], [01011010101001], [10101.000000], [393993.000000], [24/12/2018]

更新2:

代码更新:

printf("创建客户资料\n==========================\n");

printf("Name: ");
fgets(customer.name, 50, stdin);

printf("Billing Address: ");
fgets(customer.billing_address, 100, stdin);

printf("Phone Number: ");
scanf("%s", customer.phone_number);

printf("Amount Paid: ");
scanf("%lf", &customer.amount_paid);

printf("Amount Due: ");
scanf("%lf", &customer.amount_due);

printf("Payment Date: ");
scanf("%s", customer.date);

输入:

CREATE A CUSTOMER PROFILE
=========================
Name: Billing Address: test
Phone Number: 011111
Amount Paid: 10000
Amount Due: 50000
Payment Date: 12/05/2019
The record for the customer has been saved successfully!
Process finished with exit code 0

输出:

[
], [test
], [011111], [10000.000000], [50000.000000], [12/05/2019]

我按照建议使用了fgets,但是正如您所看到的,对于名称,它只读取换行符。我该如何解决这个问题?

最佳答案

scanf (and family)“%s” 格式读取空格分隔单词。如果您想读取多个单词,则需要使用 "%[" 格式(可能相当复杂),或者使用 fgets 读取整行。 .

请记住,之前的 scanf 调用会将结束换行符留在缓冲区中,因此您需要在调用 fgets 之前读取并丢弃它。

关于c - 为什么C跳到下一个printf而不是等待scanf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51405209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com