Int and Doulbe logical problem.

Pages: 12
Hello.

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;

double RT;
RT = XX/19;
cout >> "RT is: "
cout RT
system("PAUSE")
return 0;
}

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?

Please help.
Kraven.
Last edited on
apart from from missing semi-colons, your << and >> are all pointing the wrong way !

Your code should look like this:
1
2
3
4
5
6
7
8
9
10
    int XX;
    cout << "Enter Number";
    cin >> XX;

    double RT;
    RT = XX/19;
    cout << "RT is: ";
    cout << RT;
    system("PAUSE");
    return 0;


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.

But I don't understand. I mentioned that the second variable MUST be a double. So why should I put in 19.0, instead of just 19?

I'm confused :(
XX/19 = int/int = int

In an operation is taken as return type the largest one, if they are all ints you will get an int.

XX/19.0 = int/double = double
I think even this is correct:
double(XX)/19 = double/int = double
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.
You can also cast either the dividend or the divisor to a double!

(double)XX/19 will work, as well as XX/(double)19

Casting converts the value right after your cast to the type you want it to be.

PS I hope I haven't offended anyone here by C-style casting..!
Well I think is much faster typing (double)XX/19 instead of static_cast<double>(XX)/19
Uhh, I'm pretty much a beginner to C++, so could someone please explain what is "casting"?

Oh and also, I'm using Code::Blocks. If I want to make Win32 programs, I will have to use Visual C++ right? Or can I make them using Code::Blocks?


Thanks. :)
Last edited on
"Casting" is converting from a type to an other (see this: http://www.cplusplus.com/doc/tutorial/typecasting.html )

You can use any IDE and any compiler to create tour Win32 applications, just include <windows.h>.
Okay, I got one final question.

If there are many different kinds of variables, why do I always have to begin any code with:

int main() ?

Why can't I write:

char main() ?

Why should it always be an integer? I don't understand that concept.
Last edited on
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 .
Last edited on
That's my question! Why should the program end by giving an integer value? Is it just a rule, or is there an explanation behind it?

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.
Okay, I got that, thanks.

But then, what if, instead of 0, I put in return 5? Because 0 would make the program exit right? So what would another number do?

I know these questions may seem quite stupid, but I am a complete beginner to C++, so please bear with me. :)
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.
@Timaster

Like some others here already mentioned: return 0 means the program executed correctly, other returnvalues means someting wend wrong
What does that mean?
Who reads that? What does the OS do when it sees a nonzero value?
(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.
Last edited on
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.
An example:

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.
Last edited on
Pages: 12