本文附带的代码是较大程序的一部分,但我在输入结构变量“标题”和“作者”时遇到问题。 该程序似乎循环它并转到下一行。另外,虽然在字符串说明符中的's'前面使用[^ \ n],但它执行相同的操作,但我读取它时使用scanf将字符串作为输入。阅读一些帖子,但无法找出问题。 刚开始C,任何帮助将不胜感激!使用scanf进行结构输入
struct books{
char title[30];
char author[30];
char subject[20];
int quantity;
int book_id;
char *category;
int count;
float price;
};
struct books book;
book.book_id=id;
printf("\n\n\t\tBook Name:\n\t\t");
scanf(" %s",book.title);
printf("\n\n\t\tAuthor:\n\t\t");
scanf(" %[^\n]s",book.author);
printf("\n\n\t\tQuantity:\n\t\t");
scanf("%d",&book.quantity);
printf("\n\n\t\tPrice:\n\t\t");
scanf("%f",&book.price);
它可能是%s之前的空格 – Luci
您的格式说明符是否确实包含'[^ \ n]'而不是'[^ \ n]'?你不希望那里有额外的空间。 '[^ \ n]'特别指定'\ n',而不是任何空格作为字符串分隔符。如果您尝试过,那么这是您应该发布的代码。当输入不包含空格或者是一个问题时它工作吗? – Clifford
[mcve],请!顺便说一句,在'[^ \ n]'之后''s是错误的,应该删除。这虽然没有任何效果,但完全是多余的。 –