Decimal number

Write your question here.
How can i write a code that if a number is demical, tell me "wrong" ?
Hello Vafomid,

You could use "modf" from the "<cmath>" header file to split a number into two parts and if the decimal part is not zero deal with that.

http://www.cplusplus.com/reference/cmath/modf/

Otherwise you just need to tell the program what to do.

Hope that helps,

Andy
Last edited on
No i shouldnot use other libraries. And i want to write a code that recognize if number is demical. With %,*,รท or anything if its possible
What does "wrong" mean? Do you mean if it's not an integer?
There are two different angles on this. For example, 1.0 is a decimal number. If you store it in a floating-point variable such as double, there would be no way to distinguish it from a floating-point variable containing the number 1 alone.

If you want to tell those apart, you'd have to do it earlier in the process, for example by capturing the value as a string and searching for the decimal point '.' character.

That's one aspect.

The other aspect is a number such as 3.14. That is also a decimal number. You might perhaps find the whole number part by casting or assigning it to a suitable integer type. Then test to see whether the result is equal to the original value. (This should work in practice though it is potentially less robust than using the library function).
Topic archived. No new replies allowed.