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

General Discussion

Aurelian Spodarec
Aurelian Spodarec
10,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

1 Answer

Steven Parker
Steven Parker
231,210 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. :smirk:

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).

You might also tell your friend about Stack Overflow, where they can ask questions themselves in a forum specifically for the C++ language.

Aurelian Spodarec
Aurelian Spodarec
10,801 Points

Thanks :)

Well, it's a programming community, there is a topic about C# or Obj-C, which If I'm correct to have similar 'logic' when using 'int' or 'float' - perhaps even Java uses that? I don't know.

Yep, that was one of the problems!

Yeah, did tell him that too :)

Learned a thing on this my self, pretty interesting. It seems these languages are extremely specific, it's like JavaScript ES6 right now with he let and const etc... more and more specific. Where's var was just easy, I bet we will see same problem in JS too :D