C语言 我想我没有从if语句中的结构变量中获取值

dwbf0jvd  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(64)

我是C编程的初学者,我正在做一个图书馆管理系统。详细是图书馆将有只有100本书的容量,我想,如果用户输入的数量大于容量的程序重新要求用户输入给定的限制,但当我运行程序,我没有得到预期的输出,我有点周英语,所以请原谅我有问题的任何错误。
问题发生在switch casecase 1.中的if condition block

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct book
{
    char name[50];
    char Authorname[20];
    int Quantity;
} bk;

static int bookno = 1;

void incrementBN()
{
    bookno++;
}
void decrementBN()
{
    bookno--;
}

int main()
{
    int capacity = 100;
    bk book[capacity];
    int TotalQuantity = 0;

    int option;

    // system("cls");
    puts("Welcome to Library Managemt");
    puts("---------------------------");
    puts("1. Add Books.");
    puts("2. View Books.");
    puts("3. Remove Books.");
    puts("4. Issue Books.");
    puts("5. View Issue Books.");
    puts("6. Return Books.");
    puts("---------------------------");
    printf("Enter your option : ");
    scanf("%d", &option);
    getchar();

    switch (option)
    {
    case 1:

        if (TotalQuantity < capacity)
        {

            char *YN = (char *)malloc(1 * sizeof(char));
            *YN = 'y';

            while (*YN == 'y' || *YN == 'Y')
            {
                printf("Enter Details for Book No %d.\n", bookno);
                printf("Enter Name of book No : ");
                gets(book[bookno].name);
                printf("\nName of book : %s \n", book[bookno].name);

                printf("Enter Name of Author : ");
                gets(book[bookno].Authorname);
                printf("Name of Author : %s \n", book[bookno].Authorname);

                printf("Enter Quantity of books : ");
                scanf("%d", &book[bookno].Quantity);
                getchar();
                printf("Quantity Available : %d and totial quantiuty is %d\n", book[bookno].Quantity, (book[bookno].Quantity + TotalQuantity));

                incrementBN();
                // issue here the compiler is not going into this if code if user enter quatity               greater than capacity
                if (capacity < (book[bookno].Quantity) + TotalQuantity)
                {
                    printf("Enter Quantity of books less than %d : ", capacity - TotalQuantity);
                    scanf("%d", &book[bookno].Quantity);
                }
                else
                {
                    TotalQuantity += book[bookno].Quantity;
                }
                printf("Do you want to add new book : (Y/N) ");
                scanf("%c", &(*YN));
                getchar();
            }

            free(YN);
        }
        else
        {
            printf("You Cannot Add new Books because Space is full.\n");
        }
        break;

    case 2:

    {
        int *index = (int *)calloc(1, sizeof(int));
        *index += 1;
        while (*index < bookno)
        {
            printf("\nName of book : %s \n", book[*index].name);
            printf("Name of Author : %s \n", book[*index].Authorname);
            printf("Quantity Available : %d \n", book[*index].Quantity);
        }
    }
    break;

    default:
        printf("Please Select the Valid option\n");
        break;
    }

    return 0;
}

我只是想练习我学到的东西,但在这里我得到了问题,但我没有得到问题,编译器正在打印用户输入的数量值,但没有执行这些值,如果块中有人能帮助我,告诉我为什么会发生这种情况?

hujrc8aj

hujrc8aj1#

我尝试了你的程序,并注意到我收到的编译器警告沿着程序的行为,下面是你的程序的重构版本。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>          /* Added so that the "toupper" function could be used to simplify Y/N character testing         */

typedef struct book
{
    char name[50];
    char Authorname[20];
    int Quantity;
} bk;

int main()
{
    int capacity = 100;
    bk book[capacity];
    int TotalQuantity = 0;
    int bookno = -1;        /* Array indices start at "0" - the initial increment of "bookno" will set this value to "0"    */
    char YN;                /* Simpler to declare the character than defining a pointer and then allocating space           */

    char work_name[50];
    char work_author[20];
    int  work_quantity;

    int option;

    while(1)                /* Enclose the process inside of a "while" loop - exits when user selects new option "7"        */
    {
        // system("cls");
        puts("Welcome to Library Managememt");
        puts("---------------------------");
        puts("1. Add Books.");
        puts("2. View Books.");
        puts("3. Remove Books.");
        puts("4. Issue Books.");
        puts("5. View Issue Books.");
        puts("6. Return Books.");
        puts("7. Exit.");                       /* Provide a simple option to end the program   */
        puts("---------------------------");
        printf("Enter your option : ");
        scanf("%d", &option);

        switch (option)
        {
        case 1:

            if (TotalQuantity < capacity)
            {
                YN = 'Y';

                while (toupper(YN) == 'Y')
                {
                    bookno++;                                           /* Increment the index counter here         */
                    printf("Enter details for book no %d.\n", bookno + 1);
                    printf("Enter name of book no %d: ", bookno + 1);
                    getchar();
                    fgets(work_name, 49, stdin);                        /* Use fgets in lieu of gets                                    */
                    for (int i = 0; i < 50; i++)                        /* Remove any leftover newline character from the data entry    */
                    {
                        if (work_name[i] == '\n')
                        {
                            work_name[i] = '\0';
                            break;
                        }
                    }
                    printf("Name of book : %s \n", work_name);

                    printf("Enter name of author : ");
                    fgets(work_author, 19, stdin);                      /* Use fgets in lieu of gets                                    */
                    for (int i = 0; i < 20; i++)                        /* Remove any leftover newline character from the data entry    */
                    {
                        if (work_author[i] == '\n')
                        {
                            work_author[i] = '\0';
                            break;
                        }
                    }
                    printf("Name of author : %s \n", work_author);

                    printf("Enter quantity of books : ");
                    scanf("%d", &work_quantity);
                    printf("Quantity available : %d and total quantiuty is: %d\n", work_quantity, (work_quantity + TotalQuantity));

                    // Issue here the compiler is not going into this if code if user enter quantity greater than capacity
                    while (capacity < (work_quantity + TotalQuantity))
                    {
                        printf("Enter quantity of books less than %d : ", capacity - TotalQuantity + 1);
                        scanf("%d", &work_quantity);
                    }

                    // Once an allowable quantity is entered, add the book information to the structure array.

                    TotalQuantity += work_quantity;

                    strcpy(book[bookno].name, work_name);
                    strcpy(book[bookno].Authorname, work_author);
                    book[bookno].Quantity = work_quantity;

                    if (TotalQuantity >= capacity)      /* Need to exit the while loop prompting for more books if capacity is reached */
                        break;

                    printf("Do you want to add new book : (Y/N) ");
                    scanf(" %c", &YN);
                }
            }
            else
            {
                printf("You cannot add new books because space is full!\n");
            }
            break;

        case 2:
        {
            printf("- - - - - - - - - - - - - - - - - -\n");
            for (int i = 0; i <= bookno; i++)                   /* This sort of listing function is done with a "for" loop */
            {
                printf("Name of book. . . .: %s \n", book[i].name);
                printf("Name of Author. . .: %s \n", book[i].Authorname);
                printf("Quantity Available.: %d \n", book[i].Quantity);
            }
        }
        break;

        case 3:
            break;      /* TBD */

        case 4:
            break;      /* TBD */

        case 5:
            break;      /* TBD */

        case 6:
            break;      /* TBD */

        case 7:
            return 0;   /* Go ahead and exit this program */

        default:
            printf("Please Select the Valid option\n");
            break;
        }
    }

    return 0;
}

我希望那些需要重构代码的部分可以本地化到程序的一个部分,但是由于我遇到的行为,我决定包含完整的重构代码。以下是一些要点。

  • 正如好的评论中所指出的,定义一个字符并使用它比为字符定义指针,为字符分配内存,用户指针引用,然后最终释放字符内存更简单。所以“*YN”只是被定义为“char YN”。
  • 添加了字符包含文件“<ctype.h>“,以允许使用通配符操作,从而使“Yes/No”测试更简单。
  • 注意,程序使用的是一个book结构数组,对数组操作进行了修改,以适应数组从索引“0”开始的事实。
  • 请注意,当程序被编译时,编译器会给出使用“gets”函数的警告。这个函数真的不应该再使用了,应该使用“fgets”函数来代替它。
  • 按照数据输入的惯例,图书信息被输入到工作变量中,一旦所有的验证都通过了,工作信息就会被存储到相应的图书结构元素中。

有了这些,下面是一个快速测试,在终端上输入几本书的信息。

craig@Vera:~/C_Programs/Console/Library/bin/Release$ ./Library 
Welcome to Library Managememt
---------------------------
1. Add Books.
2. View Books.
3. Remove Books.
4. Issue Books.
5. View Issue Books.
6. Return Books.
7. Exit.
---------------------------
Enter your option : 1
Enter details for book no 1.
Enter name of book no 1: Book #1
Name of book : Book #1 
Enter name of author : Author #1
Name of author : Author #1 
Enter quantity of books : 66
Quantity available : 66 and total quantiuty is: 66
Do you want to add new book : (Y/N) Y
Enter details for book no 2.
Enter name of book no 2: Book #2
Name of book : Book #2 
Enter name of author : Author #2
Name of author : Author #2 
Enter quantity of books : 34
Quantity available : 34 and total quantiuty is: 100
Welcome to Library Managememt
---------------------------
1. Add Books.
2. View Books.
3. Remove Books.
4. Issue Books.
5. View Issue Books.
6. Return Books.
7. Exit.
---------------------------
Enter your option : 1
You cannot add new books because space is full!
Welcome to Library Managememt
---------------------------
1. Add Books.
2. View Books.
3. Remove Books.
4. Issue Books.
5. View Issue Books.
6. Return Books.
7. Exit.
---------------------------
Enter your option : 7

此外,我在重构的代码中加入了额外的注解,试图解释和澄清这些建议的编码技术的理由。当你学习这门语言时,你会发现有很多方法可以实现你想要的功能。这只是一个建议的方法。和往常一样,搜索可用于帮助您学习语言的教程文献。

相关问题