我在C编码方面相当新。我正在尝试让下面的项目运行,但它说我的程序在输入第三个输入后停止工作。我不相信我选择使用带有全部或布尔值的while循环是接近这个的正确选择。我几乎觉得应该在某个地方添加一个if / else语句。
我已经为项目提供了基本上沿着这些方向的流程图:
获取inv1-4的输入值
代码!= - 1
如果代码是!= 1继续循环并获取金额的输入值 买/卖
否则打印inv1-4
#include <stdio.h>
int main(void)
{
int inv1;
int inv2;
int inv3;
int inv4;
int amount_purchased;
int amount_sold;
printf("Please provide beginning inventory amounts in cases between 1 and 4.\n\n");
printf("Enter the number of inventory for Piels (ID number 1): ");
scanf("%d", &inv1);
printf("\nEnter the number of inventory for Coors (ID number 2): ");
scanf("%d", &inv2);
printf("\nEnter the number of inventory for Bud (ID number 3): ");
scanf("%d", &inv3);
printf("\nEnter the number of inventory for Iron City (ID number 4): ");
scanf("%d", &
inv4);
while (inv1 != -1 && inv2 != -1 && inv3 != -1 && inv4 != -1) {
printf("\nEnter the number of cases of Piels (ID 1) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv1 = inv1 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Coors (ID 2) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv2 = inv2 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Bud (ID 3)purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv3 = inv3 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Iron City (ID 4) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv4 = inv4 + amount_purchased - amount_sold;
}
printf("Ending inventory is as follows.\n\n");
printf("Piels (ID Number 1): %d \n", inv1);
printf("Coors (ID Number 2): %d \n", inv2);
printf("Bud (ID Number 3): %d \n", inv3);
printf("Iron City (ID Number 4): %d \n", inv4);
return 0;
}
答案 0 :(得分:2)
我猜这个问题
while (inv1 || inv2 || inv3 || inv4 != -1)
你写的不是你所期望的。 (inv1 || inv2 || inv3 || inv4 != -1)
在何时评估为true
inv1
不是0,或 inv2
不是0,或 inv3
不是0,或 inv4
不是-1。你可能想要的是这些变量中的任何一个
为-1,循环必须结束。在这种情况下,正确的条件是:
while(inv2 != -1 && inv2 != -1 && inv3 != -1 && inv4 != -1)
{
...
}
请注意,在布尔代数中a && b
相当于!a || !b
。 a || b
相当于!a && !b
。
您的scanf
也是错误的,因为很多人在评论中写道。你必须
将指针传递给int
,而不是int
。而不是
scanf("%d", amount_sold);
应该是
scanf("%d", &amount_sold);
这适用于您的所有scanf
次来电,否则就是未定义的行为
并且未定义行为的结果是未定义的。
修改强>
我刚注意到你在循环结束时有一个无条件的break
。如果
你是在第一次迭代中离开循环,为什么要循环
首先?或者你忘了添加一些if(condition)
break
?
答案 1 :(得分:1)
您有一些基本的语法问题,例如
scanf("%d", &inv1)
而不是
scanf("%d", inv1)
while循环语法也是错误的。
while(inv2 != -1 && inv2 != -1 && inv3 != -1 && inv4 != -1)
而不是
while (inv1 || inv2 || inv3 || inv4 != -1)
另一个逻辑问题。 while循环将运行一次。因为你在循环结束时编写了“break”语句。所以根本不需要任何循环。只写无循环
printf("\nEnter the number of cases of Piels (ID 1) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv1 = inv1 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Coors (ID 2) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv2 = inv2 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Bud (ID 3)purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv3 = inv3 + amount_purchased - amount_sold;
printf("\nEnter the number of cases of Iron City (ID 4) purchased and sold this week.\n");
printf("Amount purchased: \n");
scanf("%d", &amount_purchased);
printf("Amount sold: \n");
scanf("%d", &amount_sold);
inv4 = inv4 + amount_purchased - amount_sold;
如果你打算这样做,它将会起作用。