Welcome to the Treehouse Community
Looking to learn something new?
Aurelian Spodarec10,789 Points
C++ | How to not get an error when entering a value 2.35?
I have this script that if you enter 2 or 4 or 9 it works, but if you enter e.g. 2.35 it throws up an error. How can I fix that or what can I do?
Bare in mind the code isn't mine but someone asked me for the help so I'll just post it here on the forum, I don't do C or C++.
Here is the GitHub repo
Steven Parker216,033 Points
Very nice of you to offer to help someone with a language you don't know, and curious you'd ask about it in a forum where it isn't taught.
However, it turns out that all the variables in this program are declared as "int" type, which is only useful for whole numbers. To store a number with a fractional part you need a "double" or "float" variable. Then they will also need to change the template used by "scanf" to input the numbers from "%d" (which is just for integers) to "%f" (float) or "%lf") (double).