C语言代码上的库菜单程序不起作用

时间:2018-07-13 07:02:36

标签: c

在我的课堂上,我被分配去制作一个图书馆菜单程序,其中包括

  

具有书名/作者/价格/发行日期的结构;   然后是具有不同选项的菜单,例如Addbook,显示,按作者搜索等

我相信我已经知道如何做到这一点,并在代码块中草拟了该草案。但是问题是代码有效,但是我只能得到书名的输入/输出(我认为可以正常工作),而且我找不到原因。我是一个编程的初学者,只用了2个月左右的时间,所以如果有人指出我的代码有问题,我将非常感激。

谢谢,这是我的代码>

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

int bookcount=0;
struct library{                                  
char name[100];
char author[100];
float price;
char date[20];
}str[100];

int main(){
    int i;
    menu();
    return 0;
}

int menu(){
    int choice,i;
    for(i=0;;i++){
        printf("\nWelcome to library menu.Please enter a choice from below-");
        printf("\n\nPress 1 to add book information\nPress 2 to Display book information\nPress 3 to search books by author name");
        printf("\nPress 4 to list the count of books\nPress 5 to find all the books for given price\nPress 0 for exit\nChoice=");
        scanf("%d",&choice);
    if(choice==0)
        break;
    else{
        switch(choice){
            case 1:
                addbook();
                break;
            case 2:
                display();
                break;
            case 3:
                searchbyauthor();
                break;
            case 4:
                listcount();
                break;
            case 5:
                findbyprice();
                break;
            default:
                printf("invalid input!\n");
        }
    }
}
return 0;
}


int addbook(){
    int n,i;
    printf("\nEnter number of books to add=");
    scanf("%d",&n);
    for(i=0;i<n;i++){
        printf("\nEnter book title=");
        gets(str[bookcount].name);
        printf("\nEnter book author=");
        gets(str[bookcount].author);
        printf("\nEnter book date=");
        gets(str[bookcount].date);
        printf("\nEnter book price=");
        scanf("%f",&str[bookcount].price);
        bookcount++;
}
return 0;
}

int display(){
    int i;
    for(i=0;i<bookcount;i++){
        printf("\nBook no %d name=",i+1);
        puts(str[i].name);
        printf("\nBook no %d author=",i+1);
        puts(str[i].author);
        printf("\nBook no %d issue date=",i+1);
        puts(str[i].date);
        printf("\nBook no %d price=%f",i+1,str[i].price);
    }
return 0;
}

int searchbyauthor(){
    char inp[100];
    int i;
    printf("\nEnter Author name to search=");
    gets(inp);

    for(i=0;i<bookcount;i++){
        if(strcmp(str[i].author,inp)==0)
            printf("\nBook name=%s",str[i].name);
    }

    return 0;
}

int listcount(){
    printf("\nnumber of books are =%d\n",bookcount);
    return 0;
}

int findbyprice(){
    float inp;
    int i;
    printf("\nEnter price to search=");
    scanf("%f",&inp);

    for(i=0;i<bookcount;i++){
        if(str[i].price==inp)
            printf("\nBook name=%s",str[i].name);
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

由于您没有发布任何输入,因此我需要弄清楚自己,只在下一次包含输入和输出采样:

1
1
name

现在让我们看看您的程序对stdin的作用

scanf("%d",&choice); // in menu() in loop, choice := 1, ie add book
scanf("%d",&n); // in addbook(), n := 1, ie. 1 book
gets(str[bookcount].name); // in addbook in loop, this will be and should be ""

scanf()“使用并丢弃所有前导空白字符”和换行符is whitespace。这就是发生的情况:

scanf("%d",&choice); // reads '1' from the input, and stops BEFORE newline
scanf("%d",&n); // discards newline and reads 1 from the input and stops before newline
gets(str[bookcount].name); // because newline is still in buffer and gets 
                           // stops at first newline, this will read empty string
// the next gets will read the `name` from stdin, as it wasn't read already

修复非常简单。仅扫描scanf("%d\n",...);行,而不扫描单个变量。