C help

I need help about this simple task.

#include<cstdio>
#include<cstdlib>
int main(void)
{
float a,b;
scanf("%f %f",&a,&b);
printf("%f",a+b);
system("pause");
return 0;
}
Input: a=2.3,b=2.4;
Output: 4.7

Compailer should print 4.7, but why is he printing 4.700000?

I use Dev-C++ 4.9.9.2
I use Dev-C++ 4.9.9.2


Please don't. It's really quite bad. http://cplusplus.com/forum/articles/36896/

printf("%f",a+b);

You have directed the printf function to print out a decimal floating point number. As such, you have been given some decimal places.

If you want it to direct the number of decimal places, use the appropriate .precision value (try %4.2f, for example, and experiment with them to get a feel for what you can do). Take a look at the printf page and see all the options available to you. http://cplusplus.com/reference/clibrary/cstdio/printf/

A better option would be to use proper C++ objects.

Last edited on
Thanks

Btw. I think Dev-C++ is good for beginners like me. It is very simple.
It is very simple.


There are other IDEs which are also very simple, but have the extra bonus of not being bad.
@ OP: I completley agree, but don't use an IDE that is old enough to start kindergarten. Try using it's more up to date spin off: http://wxdsgn.sourceforge.net/
Can you give me example?

Did you mean "A better option would be to use proper C++ objects" cin and cout?

And one more question: is cin (console input) and cout (console output)?
Last edited on
Try this. Experiment with different input values.

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
 
int main(void)
{
float a,b;
std::cin >> a;
std::cin >> b;
std::cout << (a+b);
std::cin.get();
return 0;
}
Yes it's working, but I want to know can this code be written with "scanf and printf".

Btw. Why don't you write "using namespace std" in global area? It's easier.
Last edited on
In defence of Moschops not using the std namespace, this is pretty difficult to name a specific reason. But as someone who does the same the first time it burns you and has you tearing your hair out for hours when it's 1:00 AM and no one is on the forms to help you; you'll never use it again.
Thank you Computergeek1 for this link http://wxdsgn.sourceforge.net/.
Now I'am using Dev-C++ 7.3.1.
I didn't knew that is existing 7.3.1.

So thank you very much!!
It's easier.


Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
Lol! That is the best, and probably most accurate, analogy I've ever heard regarding the use of Bloodshed Dev-C++.
I don't agree with this statement "Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :) ". LOL :=)
This Dev-C++ 7.3.1 is 100 times better than 4.9.9.2.
Thank you guys!!!
Last edited on
It's wxDev-C++. Big difference :)
Could you explain different beetwen wxDev-C++ and 7.3.1.
I don't understand
To a degree I'm joking around, but they are distinctly different programmes.

Dev-C++ was mostly written and maintained by Colin Laplace and chums until early 2005. It used the MinGW port of the GNU C++ compiler, which was bundled with Dev-C++. As such, if you download Dev-C++, you're going to have a six year old C+ compiler. It's possible to update the compiler, but it's a bit of a pain, and if you put in that much effort you might as well switch away from Dev-C++ entirely.

wxDev-C++, courtesy of Guru Kathiresan and team, aimed to fill the hole left behind by the cessation of Dev-C++. It is under development, and I understand comes with a recent compiler, which makes a big difference. It has some features to make using wxWidgets easier.
Thank you very much Moschops on this post.Now I know different
Topic archived. No new replies allowed.