gpt4 book ai didi

c - 为什么我的代码不返回任何东西?

转载 作者:太空宇宙 更新时间:2023-11-04 07:17:47 25 4
gpt4 key购买 nike

假设对于一个条目,例如:

1 1 524 5 true -1

where the first parameter is the idOrder, the second parameter is the number of products in idOrder, the third parameter is a code, the fourth parameter is the amount of products, the fifth parameter is a boolean to distinguish the product and the sixth parameter is the final mark.

It should return an output such as:

524 -1 5

where the first parameter is the code of the product, the second parameter is the final mark and the third parameter is the amount of products.

This is my code:

#include <stdio.h>

typedef enum {false, true} bool;

int main()
{
const int END = -1;

int idOrder, numProducts, idCodeProduct, amount, total, temp;
bool generic, endSeq;

scanf("%d", temp);
/*printf("%d ", temp);*/
idOrder = temp;
endSeq = temp == END;

if (endSeq != true) {
total = 0;
scanf("%d", &temp);
numProducts = temp;
scanf("%d", &temp);
idCodeProduct = temp;
scanf("%d", &temp);
amount = temp;
scanf("%d", &temp);
generic = temp;

if (generic == true) {
total = total + amount;
printf("%d", idCodeProduct);
}
}
printf("%d ", END);
printf("%d ", total);
return 0;
}

当我运行这段代码时,它根本没有返回任何东西,我也不知道为什么。

最佳答案

问题是您在 scanf 的格式代码中有一个尾随空格。虽然这会导致 scanf 读取并丢弃空白,但它也会导致 scanf 读取直到找到非空白。如果没有更多内容可读,scanf 将无限期地等待更多输入。

删除格式中的尾随空格,效果会更好。只要您要扫描普通字符串或数字,就必须记住 scanf 实际上会自动跳过前导空格。

我建议您阅读例如this scanf reference .

关于c - 为什么我的代码不返回任何东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23198744/

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