gpt4 book ai didi

库存C程序,啤酒库存的while循环

转载 作者:行者123 更新时间:2023-11-30 19:11:32 25 4
gpt4 key购买 nike

我似乎无法停留在 while 循环中。我输入 ID 代码和第一次购买或出售的号码,它就会退出,尽管我要求只有在代码中输入 -1 时它才会退出。这里有一些简单的东西需要修正还是太广泛了?

#include <stdio.h>

int main () {

int inv1, inv2, inv3, inv4, amount, code;

printf ("Beer brand IDs \n");
printf ("1. Piels \n");
printf ("2. Coors \n");
printf ("3. Bud \n");
printf ("4. Iron City \n");

printf ("Inventory at beginning of week \n");

printf ("1. Piels: ");
scanf ("%d", &inv1);
printf ("2. Coors: ");
scanf ("%d", &inv2);
printf ("3. Bud: ");
scanf ("%d", &inv3);
printf ("4. Iron City: ");
scanf ("%d", &inv4);

while (code != -1) {
printf ("ID: ");
scanf ("%d", &code);
printf ("amount requested or subtracted: ");
scanf ("%d", &amount);

if (code == 1) {
inv1 = inv1 + amount;
}
else if (code == 2) {
inv2 = inv2 + amount;
}
else if (code == 3) {
inv3 = inv3 + amount;
}
else if (code == 4) {
inv4 = inv4 + amount;
}
else (code == -1); {
break;
}
printf ("End of week for Piels: %d", inv1);
printf ("End of week for Coors: %d", inv2);
printf ("End of week for Bud: %d", inv3);
printf ("End of week for Iron City: %d", inv4);
}

return (0);
}

最佳答案

您的代码存在许多技术逻辑问题。首先,进入循环时使用未初始化code。您的编译器应该就该问题向您发出警告。确保每次编译时都启用警告(例如-Wall -Wextra),并且不接受编译时带有警告的代码。 (您还可以添加 -pedantic 以查看其他警告)

你有一个乱码elseelse (code == -1); 不正确。看来您打算 else { code = 1;休息; }。请注意,根本不需要 code 变量。只需使用 while (1)for (;;) 进行循环,直到break 跳出循环。

您的提示显示减去,但您正在代码中添加(例如inv4 = inv4 + amount;)。只需使用 -= 运算符即可实际减去每个 IDamount

始终,始终检查每次调用 scanf返回。这是您可以验证您从用户那里收到实际值并且不会尝试从该点开始处理垃圾的唯一方法(它还可以防止无限循环,如果您的用户输入的不是小数)

最后,您的“End of week” printf语句应该外部循环,而不是内部。下面是一个简短的示例,说明您对代码的意图:

#include <stdio.h>

int main (void) {

int inv1, inv2, inv3, inv4;
inv1 = inv2 = inv3 = inv4 = 0; /* initialize values */

printf ("Beer brand IDs \n");
printf ("1. Piels\n2. Coors\n3. Bud\n4. Iron City\n\n");

printf ("Inventory at beginning of week\n");

printf ("1. Piels : ");
if (scanf ("%d", &inv1) != 1) {
fprintf (stderr, "error: invalid input.\n");
return 1;
}
printf ("2. Coors : ");
if (scanf ("%d", &inv2) != 1) {
fprintf (stderr, "error: invalid input.\n");
return 1;
}
printf ("3. Bud : ");
if (scanf ("%d", &inv3) != 1) {
fprintf (stderr, "error: invalid input.\n");
return 1;
}
printf ("4. Iron City: ");
if (scanf ("%d", &inv4) != 1) {
fprintf (stderr, "error: invalid input.\n");
return 1;
}

while (1) {
int amount = 0, code = 0;
printf ("ID ('q' to quit): ");
if (scanf ("%d", &code) != 1) break;
printf ("amount requested or subtracted: ");
if (scanf ("%d", &amount) != 1) break;

if (code == 1)
inv1 -= amount;
else if (code == 2)
inv2 -= amount;
else if (code == 3)
inv3 -= amount;
else if (code == 4)
inv4 -= amount;
else
fprintf (stderr, "warning: ID out of range.\n");
}

printf ("\nEnd of week for Piels : %d\n", inv1);
printf ("End of week for Coors : %d\n", inv2);
printf ("End of week for Bud : %d\n", inv3);
printf ("End of week for Iron City : %d\n", inv4);

return (0);
}

示例使用/输出

$ ./bin/rfmt
Beer brand IDs
1. Piels
2. Coors
3. Bud
4. Iron City

Inventory at beginning of week
1. Piels : 10
2. Coors : 10
3. Bud : 10
4. Iron City: 10
ID ('q' to quit): 1
amount requested or subtracted: 2
ID ('q' to quit): 2
amount requested or subtracted: 3
ID ('q' to quit): 3
amount requested or subtracted: 4
ID ('q' to quit): 4
amount requested or subtracted: 5
ID ('q' to quit): q

End of week for Piels : 8
End of week for Coors : 7
End of week for Bud : 6
End of week for Iron City : 5

仔细检查一下,如果您还有其他问题,请告诉我。

关于库存C程序,啤酒库存的while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39921536/

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