如何将输入更新到库存中并在以后显示?

时间:2016-11-30 18:43:37

标签: c

我目前在这里有一段代码,我卡住了。我的问题是,在我使用添加功能将游戏添加到库存后,我按P显示,但它显示库存为空。那么如何更新此库存中的新条目?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 40


typedef struct {
    char gname[30];
    char gcomp[30];
    double gprice;
} game;

void showMenu(char *choice);
void gameList(game list[], int num);
void HardCodeSix(game list[]);
void Add(game list[], int num);

int main()
{
    game list[SIZE];
    int count = 0;
    char choice;
    int num = 0;

    HardCodeSix(list);
    count = 6;

    printf("Welcome to Mini Game-Stop!!\n");
    printf("\n");
    showMenu(&choice);

    choice = toupper(choice);
    printf("Your choice is: %c\n", choice);
    printf("\n");

    while (choice != 'Q')
    {
        if (choice == 'P')
        {
            gameList(list, num);
        }
        else if (choice == 'A')
        {
            Add(list, num);
        }

        showMenu(&choice);
        choice = toupper(choice);
        printf("Your choice is: %c\n", choice);
        printf("\n");


    }
    printf("Thank for using this program. Bye!\n");
    return 0;
}
void showMenu(char *choice)
{
    printf("** Options: \n");
    printf("P....Print list of games.\n");
    printf("A....Add more game into the list.\n");
    printf("C....Clear all of the choices.\n");
    printf("D....Delete one game from the list.\n");
    printf("U....Update.\n");
    printf("Q....Quit.\n");
    printf("\n");
    printf("Please enter your choice:  ");
    scanf(" %c", &*choice);
}

void gameList(game list[], int num)
{
    int i;
    if (num == 0)
    {
        printf("Empty List!\n");
        printf("\n");
    }

    for (i = 0; i < num; i++)
    {
        printf("Title: %s\n", list[i].gname);
        printf("Developer: %s\n", list[i].gcomp);
        printf("Price: $%.2f\n", list[i].gprice);
    }
}

void Add(game list[], int num)
{
    printf("Enter game's title: ");
    scanf("%s", list[num].gname);

    printf("Enter game's developer: ");
    scanf("%s", list[num].gcomp);

    printf("Price: ");
    scanf("%lf", &list[num].gprice);
    printf("\n");
}

0 个答案:

没有答案