每当我使用Visual Studio 2015构建程序时,它都会说失败,但是当我立即重建之后,它就说成功了。另一个问题是如何存储SKU,价格和价格的多个输入,然后正确输出。

#include <stdio.h>
#define MAX_ITEMS 10

struct Item
{
    int sku_[10];
    int quantity_[10];
    float price_[10];
};

int main(void)
{
    int size = 0;
    int input =1;
    int i;
    int j;

    struct Item items[10];

    printf("Welcome to the Shop\n");
    printf("===================\n");
    printf("Please select from the following options\n");

    while (size <= MAX_ITEMS && input != 0)
    {
        printf("1) Display the inventory.\n2) Add to the inventory.\n0)Exit.\n);
        printf("Select:");
        scanf_s("%d", &input);

        while (input < 0 || input >2 && input != 0)
        {
            printf("Invalid input, try again: Please select from the following);
            printf("1)Display the inventory.\n2)Add to the inventory.\n0) Exit.\n");
            printf("Select:");
            scanf_s("%d", &input);
        }

        if (input == 1)
        {
            printf("Inventory\n");
            printf("====================\n");
            printf("Sku    Price    Quantity\n");
            printf("%d", items[size].sku_);
        }
        else if (input == 2)
        {
            printf("Please input a SKU number:");

            if (size >= MAX_ITEMS)
            {
                printf("The inventory is full");
            }
            else if (size < MAX_ITEMS)
            {
                scanf_s("%d", &items[size].sku_);
                printf("Quantity:");
                scanf("%d", &items[size].quantity_);
                printf("Price:");
                scanf("%f", &items[size].price_);
                printf("The item is successfully added to the inventory.\n");
                size += 1;
            }
        }
        else if (input == 0)
        {
            printf("Good bye");
        }
    }
}

最佳答案

以下是在源代码中检测到的错误:

1-正如WhozCraig所建议的,两个printf()调用被错误终止。

代替:

printf("1) Display the inventory.\n2) Add to the inventory.\n0)Exit.\n);
...
printf("Invalid input, try again: Please select from the following);


添加文本终止符:

printf("1) Display the inventory.\n2) Add to the inventory.\n0)Exit.\n");
...
printf("Invalid input, try again: Please select from the following");


2-输入items [size] .sku_或.quantity_或.price_时,请使用指向值的指针,而不是指向值数组的指针。结构项格式不正确。

只需修改struct Item:

struct Item
{
    int sku_; // unexpected use [10];
    int quantity_; // unexpected use [10];
    float price_; // unexpected use [10];
};


3-打印清单时,请使用循环而不是最后一个索引。并格式化每个项目的所有属性[i]以与标题对齐。

printf("Sku    Price    Quantity\n");
for(i=0;i<size;i++) {
    printf("%6d %8d %6.2f\n", items[i].sku_,items[i].quantity_,items[i].price_);
}

关于c - 我的程序在构建时失败,而在重建时成功吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40310684/

10-14 17:15