我似乎无法留在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);
}
答案 0 :(得分:1)
您的代码存在许多技术逻辑问题。进入循环时,使用 未初始化 时,首先使用code
。您的编译器应该警告您有关该问题。确保每次编译时都有 警告启用 (例如-Wall -Wextra
),并且不接受编译警告的代码。 (您还可以添加-pedantic
以查看其他警告)
你有一个乱码else
。 else (code == -1);
不正确。您似乎打算else { code = 1; break; }
。请注意,根本不需要code
变量。只需使用while (1)
或for (;;)
循环,直到break
退出循环。
您的提示说subtracted
,但您要在代码中添加(例如inv4 = inv4 + amount;
)。只需使用-=
运算符为每个amount
实际减去ID
。
始终,请务必检查每次return
来电的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
仔细看看,如果您有其他问题,请告诉我。