简体   繁体   English

C - 实践 - 结构和 Function - 输入错误

[英]C - Practice -Structs and Function - input error

I'm practicing C using structs and functions.我正在使用结构和函数练习 C。 When I ask the user to input the information about book, the code runs well till I ask to input the publisher name, here I'm always getting return EXIT_FAILURE no matther what is the input.当我要求用户输入有关书籍的信息时,代码运行良好,直到我要求输入出版商名称,这里我总是得到返回 EXIT_FAILURE 无论输入是什么。 I think that I'm doing some error the way I write the variable in the scanf because is a struct inside another struct and I still don't get this matter perfectly.我认为我在scanf中编写变量的方式出现了一些错误,因为它是另一个结构中的结构,我仍然不能完美地理解这个问题。

Bellow the code (it isn't complete):下面是代码(它不完整):

#define MAXB 2
#define MAXSTR 100

typedef struct date // publication date
{
 int day, month, year;   
    
}DATE;

typedef struct authors
{
    char authorName[MAXSTR];
    char authorwebp[MAXSTR];
    DATE birthDate;
}AUTHOR;

typedef struct publishers
{
    char publisherName[MAXSTR];
    char publishAddress[MAXSTR];
}PUBLISHER;

typedef struct books
{
    char title[MAXSTR];
    char type[MAXSTR];
    PUBLISHER publisher;
    AUTHOR author[5];
    DATE publishDate;
}BOOK;

void add_book( BOOK *b )
{
if (scanf(" %[^\n]s", b->publisher.publisherName) !=1);  // always getting EXIT_FAILURE
            return EXIT_FAILURE;
}
int main()
{
    BOOK b[MAXB];
    add_book(b);
}

Thank you in advance for your help.预先感谢您的帮助。

"if (scanf(" %[^\n]s", b->publisher.publisherName);=1); <-remove the semicolon " "if (scanf(" %[^\n]s", b->publisher.publisherName);=1); <-去掉分号"

Question answered by @ser3121023, @Luke_ and @Alejandro Montilla. @ser3121023、@Luke_ 和 @Alejandro Montilla 回答了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM