[英]Bad input C vs C++ cin vs scanf
Im a total beginner in C and here is my problem. 我是C语言的初学者,这是我的问题。 In C++ I would look for bad input(not unsigned int) this way:
在C ++中,我将以这种方式寻找错误的输入(不是unsigned int):
long double c;
cin >> c;
if (c == (unsigned int) c) {
cout<<"OK";
}
else
cout<<"NOT OK";
However when now learning C I'm trying to do it the same way, but it does not work: 但是,当现在学习C时,我试图以相同的方式进行操作,但是它不起作用:
long double c;
scanf("%lf", &c);
if (c == (unsigned int) c) {
printf("OK\n");
}
else
printf("NOT OK\n");
Any advice on how to fix it? 关于如何解决它的任何建议? My task is to do this by just using scanf and no strings.
我的任务是仅使用scanf而不使用字符串来执行此操作。 I don't want negative and float numbers to be inputted.
我不希望输入负数和浮点数。 Why doesnt this work?
为什么不起作用?
The conversion directive %lf
is for scanning double
s. 转换指令
%lf
用于扫描double
。
To scan long double
s, use %Lf
, with an upper case L
. 要扫描
long double
s,请使用%Lf
和大写L
From the specification of fscanf
(7.21.6.2) 根据
fscanf
的规范(7.21.6.2)
l (ell) Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to
long int
orunsigned long int
;l (ell)指定以下d,i,o,u,x,X或n转换说明符适用于类型指针为
long int
或unsigned long int
; that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer todouble
;a,A,e,E,f,F,g或G转换说明符的后跟适用于类型指针为
double
的参数 ; or that a following c, s, or [ conversion specifier applies to an argument with type pointer towchar_t
.或以下c,s或[转换说明符适用于类型指针指向
wchar_t
的参数。L Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to
long double
.L指定以下a,A,e,E,f,F,g或G转换说明符适用于类型指针为
long double
的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.