For循环不应该向后打印,解决方案上有任何输入吗?

时间:2019-08-23 02:09:11

标签: c for-loop

我试图为最终项目递增打印客户编号和销售阵列“ 1-4”和“ 1000-4000”。而是打印出“ 4-1”和“ 4000-1000”。关于我做错了什么的任何输入吗?

    printf("How many customers do you want to track?\n");
    scanf("%d", &ctracked);

        while((ctracked!=0)&&(ctracked--))
        {
        printf("Enter customer number. ");
        scanf("%d", &cnumber[ctracked]);
        printf("Enter the sales for the customer ");
        scanf("%d", &csales[ctracked]);
        }

    printf("Sales for the Customer\nCustomer Customer\n");
    printf("Number   Sales\n");

    for(counter=0; counter<ALLOWED_RANGE; counter++)
    {
        printf("%d        %d\n", cnumber[counter], csales[counter]);
    }

    totalsales = csales[0] + csales[1] + csales[2] + csales[3];
    avgsales = totalsales / (float)4.0;
    printf("\nTotal sales are $%d\n", totalsales);
    printf("Average sales per customer is $%.2f", avgsales);
}

1 个答案:

答案 0 :(得分:1)

只需使用以下代码更改while循环“ while((ctracked!= 0)&&(ctracked--))”。

for(counter=0;counter<ctracked;counter++)
{
    printf("Enter customer number. ");
    scanf("%d", &cnumber[counter]);
    printf("Enter the sales for the customer ");
    scanf("%d", &csales[counter]);
}