c - C-fgets 或 scanf
问题描述
for(i=0;i<requestnum;i++)
{
bookcount++;
printf("\n\nEnter Booking Request #%d", bookcount);
printf("\nLast Name:");
fgets(OneClient[i].LastName,100,stdin);
if(!strcmp(OneClient[i].LastName, "\n"))
{
printf("Processed %d successful requests out of %d submitted
requests.\nGenerating management report.\nThank you for using the hotel
reservation system.\nBye!",succescount, bookcount);
exit(1);
}
printf("First Name:");
scanf(" %s", OneClient[i].FirstName);
}
fgets 在第一个循环中完成它的工作,但是当第二个循环发生时,它会扫描并存储一个空白字符并且不等待用户输入,我使用 fgets 因为我需要在用户输入空白时终止循环. 请帮助解决我的问题?
解决方案
fgets
正在阅读流中\n
遗漏scanf
的内容。
只需替换scanf
为fgets
.
for(i=0;i<requestnum;i++)
{
bookcount++;
printf("\n\nEnter Booking Request #%d", bookcount);
printf("\nLast Name:");
fgets(OneClient[i].LastName,100,stdin);
......
.....
printf("First Name:");
/*Assumed FirstName is not pointer*/
fgets(OneClient[i].FirstName, sizeof(OneClient[i].FirstName), stdin);
}
推荐阅读
- php - 在 PHPMailer 中选择 $mail 值的随机数组
- javascript - 二十一点经销商机器人正在运行时,交易按钮不应该工作
- ios - 每次我重新点击文本字段时,视图都会发生变化
- javascript - “数字”类型的参数不可分配给“从不”类型的参数,为什么我会收到此错误?
- python - 如何为在 Tensorflow 2.x 中实现的 BERT 模型预处理数据集?
- javascript - 流式导入时 Snowpack 构建失败
- java - 事件循环应用程序(Netty、NodeJS)和多线程应用程序(.NET Core 3+、Tomcat)
- swift - AVCaptureVideoDataOutput 删除重复帧
- python - Exchangelib - 我如何知道帐户是否具有管理权限?
- javascript - 更改 webgl 供应商并使用 javascript 扩展进行渲染