【问题标题】:struct outputs are being overwritten by last user input结构输出被最后一个用户输入覆盖
【发布时间】:2017-03-05 04:44:50
【问题描述】:

我是 C 的初学者,但我在使用结构时遇到了问题。 在我向用户询问所有属性之后,我想打印结构的所有值。问题是,在我将所有属性输入到一个结构并再次循环返回第二次请求属性后,第一个结构输入被第二个结构输入替换。 我很确定我一遍又一遍地分配相同的内存空间,从而导致问题,但我坚持如何解决它。我很感激任何关于我能做什么的建议。谢谢!

                    case 2:
                        printf ("Please input a SKU number:");
                            scanf ("%d", &item[MAX_ITEMS].sku_);
                        printf ("Quantity:");
                            scanf ("%d", &item[MAX_ITEMS].quantity_);
                        printf ("Price:");
                            scanf ("%f", &item[MAX_ITEMS].price_);
                        printf ("The item is successfully added to the inventory");
                    break;

打印出sku、数量和价格

                switch (menuSelection) {
                    case 1:
                        printf ("Inventory\n");
                        printf ("=========================================\n");
                        printf ("Sku         Price       Quantity\n");

                                for (i =0 ; i<=MAX_ITEMS; i++){
                                printf ("%d %.2f %d\n", item[i].sku_, item[i].price_, item[i].quantity_);
                                }

                        printf ("=========================================\n");
                    break;

这是我的全部代码:

#include <stdio.h>
#define MAX_ITEMS 10

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


int main (void) {


int size=0;
int menuSelection;
int i=0;

    printf ("Welcome to the Shop\n");
    printf ("===================");

    do {
    printf ("\nPlease Select from the following options:\n");
    printf ("1) Display the inventory.\n");
    printf ("2) Add to shop.\n");
    printf ("0) Exit.\n");

    printf ("select:");
    scanf ("%d", &menuSelection);

            if (menuSelection <0 && menuSelection >2){
              printf ("Invalid input, try again: Please select from the following options:");
            }

            else {

                switch (menuSelection) {
                    case 1:
                        printf ("Inventory\n");
                        printf ("=========================================\n");
                        printf ("Sku         Price       Quantity\n");

                                for (i =0 ; i<=MAX_ITEMS; i++){
                                printf ("%d %.2f %d\n", item[i].sku_, item[i].price_, item[i].quantity_);
                                }

                        printf ("=========================================\n");
                    break;

                    case 2:
                        printf ("Please input a SKU number:");
                            scanf ("%d", &item[size].sku_);
                        printf ("Quantity:");
                            scanf ("%d", &item[size].quantity_);
                        printf ("Price:");
                            scanf ("%f", &item[size].price_);
                        printf ("The item is successfully added to the inventory");
                    break;

                    case 3:
                    break;
                }

            }

    } while (menuSelection != 0);


return 0;
}

【问题讨论】:

    标签: c arrays loops struct dynamic-memory-allocation


    【解决方案1】:

    您创建了一个长度为MAX_ITEMSItem 对象数组,当前为10。也就是说,您的对象具有从09 的索引。然而,当要求用户输入时,您总是将数据存储在item[MAX_ITEMS],这超出了您的数组范围。

    附带说明,在打印数组时,总是将其整个打印出来,这也意味着未初始化的项目。

    您必须存储已“添加到商店”的商品数量,并使用此数字确定必须存储用户输入的下一个数组索引。打印时,您只需遍历已存储的项目。不要忘记边界检查,例如当您的商店已满时,不允许新用户输入。

    【讨论】:

      【解决方案2】:

      问题是您总是将新值保存在同一个地方:

      item[MAX_ITEMS].sku_
      

      相反,您应该有一个计数器来显示存储了多少项目并将新值保存在与计数器相等的位置:

      item[counter].sku_
      

      每次插入后你应该增加计数器:

      counter++;
      

      所以,你的代码应该是这样的:

      int counter=0;
      ...
      case 2:
          printf ("Please input a SKU number:");
          scanf ("%d", &item[counter].sku_);
          printf ("Quantity:");
          scanf ("%d", &item[counter].quantity_);
          printf ("Price:");
          scanf ("%f", &item[counter].price_);
          printf ("The item is successfully added to the inventory");
          counter++;
      break;
      

      希望对你有帮助

      【讨论】:

      • 谢谢!!代码现在工作正常,我知道我在一开始就初始化了 size 变量。我最终使用 size=0 和 size++ 作为“计数器”,并用 i 替换了其他 max_item 实例。
      【解决方案3】:

      你定义的item是一个数组,大小为MAX_ITEMS,所以你的问题不是结构而是数组。

      在大多数计算机编程语言中,索引数组应该使用从零开始的偏移量。也就是说,item[MAX_ITEMS]MAX_ITEMS 超出了数组的范围,您遇到了一个错误并且没有找到它。当您将商品添加到您的商店时,您应该编写如下代码:

      case 2:
        if (last < MAX_ITEMS - 1)
        {
          printf ("Please input a SKU number:");
          scanf ("%d", &item[last].sku_);
          // ...
          last ++; // on success
        }
        else
        {
          print("oops, shop is full.");
        }
      

      【讨论】:

        猜你喜欢
        • 2018-08-12
        • 1970-01-01
        • 2013-10-03
        • 2022-01-04
        • 2013-04-28
        • 1970-01-01
        • 2019-05-23
        • 2021-11-13
        • 2014-12-22
        相关资源
        最近更新 更多