Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Aurelian Spodarec10,801 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 Parker229,005 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).