9阅网

您现在的位置是:首页 > 知识 > 正文

知识

c - 有没有更好的方法,让C语言从控制台接受结构中的用户全名?

admin2022-11-07知识17

这是一个图书馆管理系统的小型项目。问题是,用户输入的第一个fgets函数在第一种情况下无论内容如何都会失败,但后面的都可以。我想从控制台接受包含空格的书名和作者的全名。

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

// Structure definition
struct library
{
    char bookName[50];
    char authorName[50];
    int numberOfPages;
    float price;

};

int main()
{
    // Structure Variable declaration
    struct library lib[50];
    // Variables initialization
    int i,j,keepcount;
    i=j=keepcount = 0;
    // Character arrays
    char arth_nm[50],book_nm[50];


    while(j!=7)
    {
        // Menu Selection
        printf("\n\n1. Add book information\n2. Display book information\n");
        printf("3. List all books of given author\n");
        printf("4. List the title of specified book\n");
        printf("5. List the count of books in the library\n");
        printf("6. Display Highest Price Book\n");
        printf("7. Exit");
        printf ("\n\nSelect one of the above: \n");
        scanf("%d",&j);


        switch(j)
        {
        // Entering book details
         case 1:
            printf ("Enter book name:  ");
            fgets(lib[i].bookName, sizeof(lib[i].bookName), stdin);

            printf ("Enter author name: ");
            fgets(lib[i].authorName, sizeof(lib[i].authorName), stdin);

            printf ("Enter pages: ");
            scanf ("%d",&lib[i].numberOfPages);

            printf ("Enter price: ");
            scanf ("%f",&lib[i].price);

            keepcount++;
        break;

       // All book details entered
        case 2:
            printf("You have entered the following information\n");
            for(i=0; i<keepcount; i++)
            {
                printf ("Book name = %s",lib[i].bookName);

                printf ("\tAuthor name = %s",lib[i].authorName);

                printf ("\tPages = %d",lib[i].numberOfPages);

                printf ("\tPrice = %f",lib[i].price);

                printf("\n");
            }

        break;

        // Searching for book details by using the name of the Author
        case 3:
        printf ("Enter author name : ");
        scanf ("%s",arth_nm);
        for (i=0; i<keepcount; i++)
        {
            if (strcmp(arth_nm, lib[i].authorName) == 0)
            printf ("%s %s %d %f",lib[i].bookName,lib[i].authorName,lib[i].numberOfPages,lib[i].price);
        }
        break;

        // Searching for book details by using the name of the book
        case 4:
        printf ("Enter book name : ");
        scanf ("%s",book_nm);
        for (i=0; i<keepcount; i++)
        {
            if (strcmp(book_nm, lib[i].bookName) == 0)
            printf ("%s \t %s \t %d \t %f",lib[i].bookName,lib[i].authorName,lib[i].numberOfPages,lib[i].price);
        }
        break;
        // Case for Total many of books shelved
        case 5:
        printf("\n No of books in library : %d", keepcount);
        break;

        // Case for Highest paid book
        case 6:
        printf ("Highest Price Book : ");
        float temp = 0;
        for (i=0;i<keepcount;i++)
        {
            if(temp < lib[i].price)
                temp = lib[i].price;
        }
        printf("%f", temp);

        break;

        case 7:
        exit (0);


    }


    }

    return 0;
}


【回答】:

使用 fgets() 语句来接受whitespaces。

fgets(arth_nm, 50, stdin);

也许你会被跳过,等待输入的是 arth_nm 因为已经 stdin 以前使用过的。您应该 fflush(stdin) 它。

fflush(stdin);
fgets(arth_nm, 50, stdin);

它会帮助你。

【回答】:

是的,有,正如@Rohan Bari所说,你可以使用 fgets() 但有些编译器不支持使用这个函数。另外,你也可以使用 scanf ("%[^\n]%*c", variableName); 这个函数会接收输入,直到按下新的一行或回车键,也就是说,它将读取任何字符,包括whitesapces.我希望我已经能够帮助。