void add_comment_to_book(int book_index) { int comment_id = num_comments; comments[num_comments].id = comment_id; comments[num_comments].book_id = book_index; printf("请输入评论:"); getchar(); // 消耗掉之前输入的回车符 fgets(comments[num_comments].text, sizeof(comments[num_comments].text), stdin); comments[num_comments].text[strlen(comments[num_comments].text)-1] = '\0'; book[book_index].commentids[book[book_index].commentcount] = comment_id; book[book_index].commentcount++; num_comments++; printf("评论成功!\n");改进用户可以多次评论
时间: 2024-03-02 08:51:37 浏览: 53
tong_xun_lu.rar_int enter_读文件 list
可以在函数中添加一个循环,让用户可以多次评论,直到用户输入指定的结束标志才退出循环,比如可以在每次评论后询问用户是否继续评论,如果用户输入是,则继续评论,如果输入否,则退出循环。具体实现可以参考下面的代码示例:
```
void add_comment_to_book(int book_index) {
int comment_id;
char continue_flag = 'Y';
do {
comment_id = num_comments;
comments[num_comments].id = comment_id;
comments[num_comments].book_id = book_index;
printf("请输入评论:");
getchar(); // 消耗掉之前输入的回车符
fgets(comments[num_comments].text, sizeof(comments[num_comments].text), stdin);
comments[num_comments].text[strlen(comments[num_comments].text)-1] = '\0';
book[book_index].commentids[book[book_index].commentcount] = comment_id;
book[book_index].commentcount++;
num_comments++;
printf("评论成功!\n");
printf("是否继续评论?(Y/N)");
scanf("%c", &continue_flag);
getchar(); // 消耗掉之前输入的回车符
} while (continue_flag == 'Y' || continue_flag == 'y');
}
```
阅读全文