【发布时间】:2015-08-08 05:43:51
【问题描述】:
如何在 c 中强制执行以下输入限制?
第一行包含 float , 第二行包含 float , 第三行 int,
在控制台中按三下回车后,程序应该能够读取每一行并将内容放入相应的 int,int,float 变量中。
三个回车键后程序不应等待用户输入并开始验证。
一些测试用例
line1: 34
line2:4
line3:12
result: ok
line1:
line2:4
line3:12
result: not ok
line1: Hi
line2:4
line3:12
result: not ok
到目前为止,我使用的是基础知识
scanf("%f",&p);
scanf("%f",&r);
scanf("%d",&t);
它适用于测试用例 1 和 3,但当我留下空行时会失败。
【问题讨论】:
-
你必须用
scanf做这个吗? -
阅读带有
fgets的行。使用sscanf或strto_系列函数进行转换。 -
我不允许使用 conio.h。仅 stdio.h :(
-
fgets-> 修剪 ->sscanf和%n -
这些函数都不在
中。 中fgets和sscanf在中。 strto_在
标签: c validation input scanf