我的程序在构建时失败并在重建时成功?

时间:2016-10-28 17:55:21

标签: c

每当我使用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 个答案:

答案 0 :(得分:1)

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

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");

添加文本终结符:

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

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

只需修改struct Item:

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_);
}

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

void Main (string[] args)
{
    #if !CMD
        args = new[] { @"arg1", @"arg2" };
    #endif
    args.Dump();
    ...