Yesterday I was learning about double floating points, and I decided to just write a simple code to try it out. Now I know that if I use "int", I cannot put in a number like 34.567. Just whole numbers. But with double I can put in decimals. I know that much.
My code was that I would define two variables, XY and RT, the first is "int" and the second is "double".XY will be user input and RT = XY/19.
I build it, everything goes fine, and then I run the program. I enter a number for XY. But the answer I get for RT isn't a decimal. Why is that?
Here's the code:
int main()
{
int XX;
cout >> "Enter Number";
cin << XX;
So basically the first is an integer and the second is a decimal value. Logically speaking, if I divide a whole number by a prime number, I SHOULD get a decimal value right? But I don't. I keep getting a round up answer. Why is this?
The answer is that xx/19 is a division of two integers. So if the user enters 25, then 25/19 will be an integer - in this case 1.
This 1 is then converted to a double and assigned to the variable RT - so RT will be
1.0000000000000000.
Now if you change it from 19 to 19.0 - then you will have an integer (25) divided by a double (19.0) - the integer 25 will get promoted to a double (25.0),
the result will be a double - ( 1.315xxxxxxxxxxxx) which will be assigned to the double RT.
In the expression XX / 19, XX is declared as type int and the literal value 19 is also assumed to be an int by the compiler. When both the divisor and dividend are integral, the compiler generates integer division code. That integral result in then put into a double (RT).
You need to, as guestgulkan said, tell the compiler to use floating point division instead of integer division. The only way to do this is to tell the compiler that either the divisor or dividend (or both) are floating point numbers.
The literal value 19.0 is treated by the compiler as a double according to the standard;
the literal value 19 is treated by the compiler as an int according to the standard.
The program should end giving an integer value (0 if everything was OK, not 0 if some errors occurred).
That's the reason for typing return 0; when you want to end your program.
Some compilers may accept different declarations of main() but the standard one is int .
The return code from main is the value that is passed to the operating system as the exit code of the process. Process exit codes are always integers, hence main() should always return integer.
I've never once heard a good explanation of why to use return 0. It's just a standard. For all intents and purposes that you're concerned with, return 5;, return -428;, and return (int)"This is a return value"; do the same thing. Just as long as you're returning some integer.
(The following statements may be wrong and are likely to be challenged)
Well I'm pretty sure the OS does nothing with the return value, it is purely for the user / coder to know what is wrong, mainly for debugging purposes. For example, if you had some function that goes wrong in some case, you want it to return a value that represents what happened. 0 if that function ran successfully and as planned, and different numbers can represent whatever you want them to!
Say you want the function to return 4 if the number was not divisible by 12, that's OK!
Again, this may not be correct, and I hope this helps you.
Yeah, AFAIK, the OS doesn't care what you return. It's basically there for either the user or the program which called the one that returned to look at.
open a .exe in command prompt and a .cpp file of that program in your IDE (in my case dev-cpp). Then try to compile the code while your running the .exe file at the same time. You will problably get a message like this:
"Permission denied
ld returned 1 exit status"
Then you know the code hasent been compiled, and you got a hint wy.