Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

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

1 Answer

Steven Parker
Steven Parker
216,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. :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,789 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