在我的类里面,我被分配制作一个图书馆菜单程序,其中包括
a structure with bookname/author/price/issue date; then a menu with different options like addbook,display, search by author etc
我相信我已经掌握了如何做的基本概念,并且我在代码块中起草了它的草稿。但问题是代码可以正常工作,但我无法仅获得 book name 的输入/输出(我想是在休息),而且我找不到原因。我是编程的初学者,刚开始编程 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
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",...);
行而不是单个变量。
关于c - C 语言代码上的库菜单程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51319419/