C循环问题

问题描述 投票:0回答:2

我在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;

}
c while-loop crash
2个回答
2
投票

我想这个问题

while (inv1 || inv2 || inv3 || inv4 != -1) 

是你写的不是你所期望的。当(inv1 || inv2 || inv3 || inv4 != -1)不为0或inv1不为0或inv2不为0或inv3不为-1时,inv4求值为true。您可能想要的是,如果这些变量中的任何一个为-1,则循环必须结束。在这种情况下,正确的条件是:

while(inv2 != -1 && inv2 != -1 && inv3 != -1 && inv4 != -1)
{
    ...
}

请注意,在布尔代数中,a && b等同于!a || !ba || b相当于!a && !b

你的scanfs也是错误的,因为许多人在评论中写道。你必须将指针传递给int,而不是int。而不是

scanf("%d", amount_sold);

它应该是

scanf("%d", &amount_sold);

这适用于所有scanf调用,否则它是未定义的行为,未定义行为的结果是未定义的。

编辑

我刚注意到你在循环结束时有一个无条件的break。如果您在第一次迭代中无论如何都要离开循环,为什么要首先进行循环?或者你忘记为if(condition)添加一些break


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;

如果它是你的意图,它将工作相同。

© www.soinside.com 2019 - 2024. All rights reserved.