c - 我的指针和结构代码显示垃圾值

标签 c

目前,我正在尝试创建一个程序来清点商店的产品。我已经能够创建一个允许用户在结构数组中输入新项目的函数,但是当我尝试打印这些值时,我得到了垃圾值。 (请忽略 switch 语句,因为代码正在进行中)。

#include <stdio.h>
#include <stdlib.h>
#define MAX_INVENTORY_SIZE 100

typedef struct {
    char item_Number[3];
    char item_Name[20];
    float item_Profit;
    float latest_Price;
    unsigned int stock;
    unsigned int total_Sold;
    struct InventoryItemType *next;
}InventoryItemType;
void MainMenu();
void displayInventory(InventoryItemType *(*));
void addItem(InventoryItemType, int i);

int main()
{
    int i=1;
    char selection;
    int count=1;
    InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE] ;

    inventoryItems[0]=NULL;
    while(1)
    {
        MainMenu();
        scanf(" %c", &selection);
        switch(selection) 
        {
        case 'A' :
            displayInventory(inventoryItems);
            break;
        case 'B' :
        case 'C' :
            inventoryItems[count]= (InventoryItemType*)malloc(sizeof(InventoryItemType));
            addItem(*inventoryItems[count], count);
            continue;
        case 'D' :
        case 'E' :
        case 'F' :
        case 'G' :
        case 'H' :
        default :
            printf("Invalid\n" );
        }
        printf("Bottom of code\n");
        system("pause");
    }
}
void MainMenu()
{
    printf("A. Display Inventory\n");
    printf("B. Display Sales\n");
    printf("C. Add Item\n");
    printf("D. Remove Item\n");
    printf("E. Enter Shipment\n");
    printf("F. Update Sales\n");
    printf("G. Sort\n");
    printf("H. Exit\n");
    printf("Make a selection\n");
}
void displayInventory(InventoryItemType *display)
{
    int i;
    for(i=0; i<MAX_INVENTORY_SIZE; i++)
    {
        printf("Name:%s\n", display[i].item_Name);
        printf("Stock:%d\n", display[i].stock);
        printf("Price:%.2f\n", display[i].latest_Price);
        printf("Total Value:%.2f\n", (display[i].stock)*(display[i].latest_Price));
        printf("\n");
    }
}
void addItem(InventoryItemType display)
{

    printf("\nEnter details of item \n\n");
    printf("Enter Item Name: \n");
    scanf("%s", display.item_Name);
    printf("\nEnter Item no: \n");
    scanf("%s", display.item_Number);

    printf("\nEnter Stock: \n");
    scanf("%d", &display.stock);

    printf("\nPurchase Price: \n");
    scanf("%f", &display.latest_Price);
}

更新

我尝试应用所有答案(通过一系列试验和错误)并得出此代码。我目前的代码正确地接受了一个新项目并显示了该项目,但是在添加更多项目后它就崩溃了(如果我继续解决错误,我会得到垃圾显示值)。我很确定这与我通过 malloc 分配内存的方式有关。我的目标是在初始化值之前使用 malloc 为项目创建空间。

编辑后的代码:

#include <stdio.h>
#include <stdlib.h>
#define MAX_INVENTORY_SIZE 100

typedef struct {
    char item_Number[4];
    char item_Name[20];
    float item_Profit;
    float latest_Price;
    float selling_Price;
    unsigned int stock;
    unsigned int total_Sold;
}InventoryItemType;
void MainMenu();
void displayInventory(InventoryItemType *, int);
void displaySales(InventoryItemType *, int);
InventoryItemType *addItem(InventoryItemType *, int);

int main()
{
    int i=0, item_count=0;
    char selection;
    InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE];
    while(1)
    {
        MainMenu();
        scanf(" %c", &selection);
        switch(selection) 
        {
        case 'A' :
            displayInventory(*inventoryItems, item_count);
            break;
        case 'B' :
            displaySales(*inventoryItems, item_count);
            break;
        case 'C' :
            inventoryItems[item_count]=(InventoryItemType*)malloc(sizeof(InventoryItemType));
            inventoryItems[item_count]=addItem(inventoryItems[item_count],item_count);
            item_count++;
            continue;
        case 'D' :
        case 'E' :
        case 'F' :
        case 'G' :
        case 'H' :
        default :
            printf("Invalid Entry\n" );
            system("pause");
        }
        system("cls");
    }
}
void MainMenu()
{
    printf("A. Display Inventory\n");
    printf("B. Display Sales\n");
    printf("C. Add Item\n");
    printf("D. Remove Item\n");
    printf("E. Enter Shipment\n");
    printf("F. Update Sales\n");
    printf("G. Sort\n");
    printf("H. Exit\n");
    printf("Make a selection\n");
}
void displayInventory(InventoryItemType *display, int key)
{
    if(display[0].item_Name == NULL)
    {
        printf("No stock");
        system("pause");
    }
    else
    {
        int i;
        for(i=0; i<key; i++)
        {
            printf("Item No.:%s\n", display[i].item_Number);
            printf("Item Name:%s\n", display[i].item_Name);
            printf("Item Stock:%d\n", display[i].stock);
            printf("Item Purchased Price:%.2f\n", display[i].latest_Price);
            printf("Total Value of Items:%.2f\n", (display[i].stock)*(display[i].latest_Price));
            printf("\n");
            system("pause");
        }
    }
}
void displaySales(InventoryItemType *display, int key)
{
    int i;
    float total_profit=0;
    for(i=0; i<key; i++)
    {
        printf("Item No.:%s\n", display[i].item_Number);
        printf("Item Name:%s\n", display[i].item_Name);
        printf("Number of Item Sold:%d\n", display[i].total_Sold);
        printf("Item Selling Price:%.2f\n", display[i].selling_Price);
        printf("Total Profit from Item:%.2f\n", (display[i].selling_Price-display[i].latest_Price)*display[i].total_Sold);
        total_profit=total_profit+((display[i].selling_Price-display[i].latest_Price)*display[i].selling_Price);
        if(i==key)
        printf("Total Over-all Profit:%.2f", total_profit);
        system("pause");
    }
}
InventoryItemType *addItem(InventoryItemType *change, int key)
{
    InventoryItemType *current= (InventoryItemType*)malloc(sizeof(InventoryItemType));
    printf("\nEnter details of item \n\n");
    printf("Enter Item no: \n");
    scanf("%s", current[key].item_Number);
    printf("Enter Item Name: \n");
    scanf("%s", current[key].item_Name);
    printf("Enter Stock: \n");
    scanf("%d", &current[key].stock);
    printf("Enter Purchase Price: \n");
    scanf("%f", &current[key].latest_Price);
    current[key].selling_Price=(current[key].latest_Price)*1.5;
    current[key].total_Sold=0;
    change=current;
    system("cls");
    return change;
}

最佳答案

存在许多错误/冲突。太多了,无法单独评论。我已经注释并更正了代码,显示了错误和之前/之后。还有更多工作要做,但这应该会让你们更接近。

#include <stdio.h>
#include <stdlib.h>
#define MAX_INVENTORY_SIZE 100

typedef struct {
    char item_Number[3];
    char item_Name[20];
    float item_Profit;
    float latest_Price;
    unsigned int stock;
    unsigned int total_Sold;
    struct InventoryItemType *next;
} InventoryItemType;

void MainMenu();

#if 0
void displayInventory(InventoryItemType *(*));
#else
void displayInventory(InventoryItemType *,int);
#endif

#if 0
void addItem(InventoryItemType, int i);
#else
void addItem(InventoryItemType *);
#endif

int main()
{
    int i=1;
    char selection;

    // NOTE/BUG: starting at 1 will leave item 0 undefined
#if 0
    int count=1;
#else
    int count=0;
#endif

    // better to do this with a stack array than pointers to malloc'ed items
#if 0
    InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE] ;
#else
    InventoryItemType inventoryItems[MAX_INVENTORY_SIZE] ;
#endif

#if 0
    // NOTE/BUG: this will segfault because inventoryItems is undefined here
    inventoryItems[0]=NULL;
#endif

    while (1) {
        MainMenu();
        scanf(" %c", &selection);
        switch(selection) {
        case 'A' :
#if 0
            displayInventory(inventoryItems);
#else
            displayInventory(inventoryItems,count);
#endif
            break;
        case 'B' :
        case 'C' :
#if 0
            inventoryItems[count]= (InventoryItemType*)malloc(sizeof(InventoryItemType));
            addItem(*inventoryItems[count], count);
#else
            addItem(&inventoryItems[count]);
            count += 1;
#endif
            continue;
        case 'D' :
        case 'E' :
        case 'F' :
        case 'G' :
        case 'H' :
        default :
            printf("Invalid\n" );
        }
        printf("Bottom of code\n");
        system("pause");
    }
}

void MainMenu()
{
    printf("A. Display Inventory\n");
    printf("B. Display Sales\n");
    printf("C. Add Item\n");
    printf("D. Remove Item\n");
    printf("E. Enter Shipment\n");
    printf("F. Update Sales\n");
    printf("G. Sort\n");
    printf("H. Exit\n");
    printf("Make a selection\n");
}

#if 0
void displayInventory(InventoryItemType display)
{
    int i;
    for(i=0; i<MAX_INVENTORY_SIZE; i++)
    {
        printf("Name:%s\n", display[i].item_Name);
        printf("Stock:%d\n", display[i].stock);
        printf("Price:%.2f\n", display[i].latest_Price);
        printf("Total Value:%.2f\n", (display[i].stock)*(display[i].latest_Price));
        printf("\n");
    }
}

void addItem(InventoryItemType display)
{

    // NOTE/BUG: because this is passed by _value_ nothing will be retained
    // after function return

    printf("\nEnter details of item \n\n");
    printf("Enter Item Name: \n");
    scanf("%s", display.item_Name);
    printf("\nEnter Item no: \n");
    scanf("%s", display.item_Number);

    printf("\nEnter Stock: \n");
    scanf("%d", &display.stock);

    printf("\nPurchase Price: \n");
    scanf("%f", &display.latest_Price);
}
#else
void displayInventory(InventoryItemType *item,int count)
{
    int i;
    for (i=0; i<count; i++, item++) {
        printf("Name:%s\n", item->item_Name);
        printf("Stock:%d\n", item->stock);
        printf("Price:%.2f\n", item->latest_Price);
        printf("Total Value:%.2f\n", (item->stock)*(item->latest_Price));
        printf("\n");
    }
}

void addItem(InventoryItemType *item)
{

    printf("\nEnter details of item \n\n");

    printf("Enter Item Name: \n");
    scanf("%s", item->item_Name);

    printf("\nEnter Item no: \n");
    scanf("%s", item->item_Number);

    printf("\nEnter Stock: \n");
    scanf("%d", &item->stock);

    printf("\nPurchase Price: \n");
    scanf("%f", &item->latest_Price);
}
#endif

关于c - 我的指针和结构代码显示垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49544117/

相关文章:

分割数组并计算重复次数的代码

c - unsigned short int 与 unsigned int 有何不同

c++ - 在 C++ 中转换为 void* 和 typedef

c - 奇偶校验如何找到偶数或奇数 1 的位?

c - 根据输入创建变量

c - 在 OpenGL 中忽略 macOS 上的模拟鼠标事件

c - 违反约束是否总是导致诊断?

c - 将 unsigned char 指针传递给 atoi 而不进行强制转换

UNIX 中的 C : Reading/combining files based upon number of bytes

c - 未定义的操作