开启编程大门 发表于 2020-4-3 22:05:11

c语言程序问题

想编写一个小型图书馆程序,但是实现不了查询功能。哪位大佬帮忙指点一下哪里的问题,万分感谢{:5_110:}

sunrise085 发表于 2020-4-3 22:46:00

字符串比较不能用==
有专门进行字符串比较的函数
strcmp(str1,str2),若两个字符串一样,返回0,不一样则返回差值,差值是首个不相同的字符的ASCII码之差
需要先 #include "string.h"

另外,请注意!发帖请不要这样大片的代码截图,请直接发代码。尤其像你这样的长程序,难道你想让别人为了回答你的出问题,去敲一遍代码么?
#include <stdio.h>
#include "string.h"
intmain()
{
    char a,b;
    scanf("%s",a);
    scanf("%s",b);
    if (strcmp(a,b))
      printf("different\n");
    else
      printf("same\n");
    return 0;
}

开启编程大门 发表于 2020-4-6 12:12:49

sunrise085 发表于 2020-4-3 22:46
字符串比较不能用==
有专门进行字符串比较的函数
strcmp(str1,str2),若两个字符串一样,返回0,不一样则 ...

抱歉,没咋发过帖子,不太懂。谢谢你的提醒以后就懂了,还有谢谢你的答案,虽然我照着改了但是好像还不行,应该是逻辑错了。

开启编程大门 发表于 2020-4-6 12:34:11

sunrise085 发表于 2020-4-3 22:46
字符串比较不能用==
有专门进行字符串比较的函数
strcmp(str1,str2),若两个字符串一样,返回0,不一样则 ...

麻烦帮忙复制看一下问题在哪 谢谢


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

int j = 1;
int how_many_book = 0; //用于 1.新书入库 统计库内有多少本书
int i = 0; //用于 2.查询书籍信息 循环
char bookname;        //被查询书的名字
int letter; //用于switch循环判断

struct Years
{
        int year;
        int month;
        int day;
}years;

struct Book //定义图书馆结构体
{
        char name;
        char writer;
        float money;
        struct Years years;
        char home;
}bookshop;

void printbook(struct Book); // 打印书籍函数
struct Book writebook(struct Book book); // 录入书籍函数
void import(); //主循环函数

int main()
{
        printf("欢迎进入图书馆程序!\n");
        while(j)
        {
                import();       
        }
        return 0;
}
       


struct Book writebook(struct Book book)
{
        printf("请输入书名:");
        scanf("%s",book.name);
        printf("请输入作者:");
        scanf("%s",book.writer);
        printf("请输入价格:");
        scanf("%f",&book.money);
        printf("请输入出版日期:");
        scanf("%d-%d-%d",&book.years.year,&book.years.month,&book.years.day);
        printf("请输入出版社:");
        scanf("%s",book.home);
        return book;
}

void printbook(struct Book b1)
{
        printf("书的名称是:%s\n",b1.name);
        printf("书的作者是:%s\n",b1.writer);
        printf("书的价格是:%.2f\n",b1.money);
        printf("书的出版日期是:%d-%d-%d\n",b1.years.year,b1.years.month,b1.years.day);
        printf("书的出版社是:%s\n",b1.home);
       
}

void import() //主循环函数
{

printf("1.新书入库\n");
printf("2.查询书籍信息\n");
printf("3.删除书籍\n");
printf("4.书籍借记\n");
printf("5.退出程序\n");
scanf("%d",&letter);
switch(int(letter))
{
        case 1: printf("请输入第%d本书的信息\n",how_many_book + 1);
                        bookshop =writebook(bookshop);
                        printbook(bookshop);
                        how_many_book ++ ;
                        break;
                                          
        case 2:
                        printf("请输入书籍的名字:\n");
                        scanf("%s",bookname);
                       
                        for(i = 0 ; strcmp(bookname,bookshop.name) == 0 || (i == how_many_book) ; i++) //名字对上或循环次数等于储存书量就退出
                        {
                                ;
                        }
                        if(i == how_many_book &&strcmp(bookname,bookshop.name) ==0 ) //循环到头且最后一个名字也不一样
                        {
                                printf("抱歉,库内暂无此书信息\n");
                        }
                        else // 除去以上情况就是找到了
                        {
                       
                                printf("此书已经找到,下面开始打印书籍信息\n");
                                printbook(bookshop); //调用打印书籍函数
                        }
        case 5:
                j = 0;
                break;
}
}

KevinHu 发表于 2020-4-6 13:06:22

WirteBook()函数里要传一个结构体指针进去,要不然修改不了原结构体
页: [1]
查看完整版本: c语言程序问题