Bad input C vs C++ cin vs scanf -


im total beginner in c , here problem. in c++ bad input(not unsigned int) way:

long double c; cin >> c;  if (c == (unsigned int) c) { cout<<"ok";     } else cout<<"not ok"; 

however when learning c i'm trying same way, not work:

long double c; scanf("%lf", &c);  if (c == (unsigned int) c) { printf("ok\n");     } else printf("not ok\n");     

any advice on how fix it? task using scanf , no strings. don't want negative , float numbers inputted. why doesnt work?

the conversion directive %lf scanning doubles.

to scan long doubles, use %lf, upper case l.

from specification of fscanf (7.21.6.2)

l (ell) specifies following d, i, o, u, x, x, or n conversion specifier applies argument type pointer long int or unsigned long int; that following a, a, e, e, f, f, g, or g conversion specifier applies argument type pointer double; or following c, s, or [ conversion specifier applies argument type pointer wchar_t.

l specifies following a, a, e, e, f, f, g, or g conversion specifier applies argument type pointer long double.


Comments