• Welcome to Valhalla Legends Archive.
 

Flame the noob day!

Started by j0k3r, July 02, 2003, 02:36 PM

Previous topic - Next topic

j0k3r

Ok right I found my "C++ for dummies" book and since I'm bored as hell I started reading it... And I stumbled across this which I haven't been able to figure out.

To my understanding (and as they previously described in the book), % (modulus) gives the remainder of a division... Then comes this example...
Quote
float f1 = 10.0;
float f2 = 100 % 10;
f1 == f2                            //are these two equal?

Theoretically, f1 and f2 should be equal ...
And it goes on... Right so my problem is this, isn't 100 % 10 result in 0? So that makes them NOT equal, and the book wrong?
Just confused as to who is wrong.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

j0k3r

Oh yes, any free compilers you know of would be nice, especially the easy to install ones.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

TheMinistered

#2
Yes, they are wrong.  If it were 100 / 10  instead, then they would be right, however the remainder is 0.

Eibro

Quote from: j0k3r on July 02, 2003, 04:01 PM
Oh yes, any free compilers you know of would be nice, especially the easy to install ones.
Check http://www.bloodshed.net
Eibro of Yeti Lovers.

j0k3r

Ah yes I found bloodshed, quite nice, however it doesn't seem to work. I found "helloworld.cpp" and opened it in bloodshed, it even came with the program, yet it doesn't work? I downloaded v4 and the extra file that it recommended I download, I fail to see the problem.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

iago

Can you give us more details?  Dev-c++ is a good way to learn, but you generally won't be able to compile source that you find with it.  

If I played with it again, I might be able to figure out why, but back when I used it I had no idea.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


j0k3r

I think it had something to do with me changing the directories I'll try again later...

Ok some more questions (actual questions this time)

What does "return 0;" do?
What are the seperate "#include <so-and-so.h>" statements and what are they used for?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Camel

#7
Quote from: j0k3r on July 03, 2003, 06:34 PM
What does "return 0;" do?
What are the seperate "#include <so-and-so.h>" statements and what are they used for?
"return 0;" makes a function return zero. Most functions you'll find will return a value (except the ones declared as 'void'). Example:
int someFunctionThatAlwaysReturnsFive()
{
    return 5;
}

void main()
{
    int setMeToFive;
    setMeToFive = someFunctionThatAlwaysReturnsFive();
    //now (int)setMeToFive is equal to 5, because the function returned five
    //main() does not need a return statement because it has been declared as void. Sometimes you will see it as 'int main(),' in which case you would need to return a value -- usually zero, usually signifying that the program has terminated without error.
}


#include is used to 'include' a header (.h) file. Generally, a header file is used to declare functions in seperate .c/.cpp/.dll files.

<five.cpp>
int someFunctionThatAlwaysReturnsFive()
{
    return 5;
}

<five.h>
int someFunctionThatAlwaysReturnsFive();

<main.cpp>
#include "five.h" //include the file that declares our function
void main()
{
    int setMeToFive;
    setMeToFive = someFunctionThatAlwaysReturnsFive();
}


Sometimes you will see #include <file.h> because it is not in the same path as your project's folder but in the 'system/compiler includes' folder.

j0k3r

Ok, graci that helps... So all the "include" files are predefined functions which you want to include in your program so you don't have to redefine them (assuming you'd need them)?

Is the only way of knowing what's in them to open them and read?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

iago

yes

And you will quickly learn the important functions, but the best thing to do is get a reference book or look up functions on msdn to know which header to include to use them.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Camel

You can usually/always find what you need by typing key words augmented by "C++" into google. If you cant figgure out where a function is included, check MSDN by typing "site:msdn.microsoft.com the_function_name" into google. :P

Adron

Quote from: j0k3r on July 03, 2003, 08:21 PM
Is the only way of knowing what's in them to open them and read?

Mostly when you look up a function in the documentation, it'll also say which header you need to include to use that function. That's true for most man pages as well as for Microsofts MSDN.

j0k3r

More questions... (sorry)

1. Is "int main()" where the program is? And is it a functoin?

2. What is the point of "return 0;" in a program... Is it just there becase it is needed in a function? Is "return 0;" the same thing as saying "return void;" (telling it not to return anything)?

3. Is there a point to prototype functions? Why wouldn't the functions just be declared in advance?

Thanks again guys, your really helping this go much less painfully than last time I tried to learn lol.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Camel

Quote from: j0k3r on July 04, 2003, 01:04 PM
More questions... (sorry)

1. Is "int main()" where the program is? And is it a functoin?

2. What is the point of "return 0;" in a program... Is it just there becase it is needed in a function? Is "return 0;" the same thing as saying "return void;" (telling it not to return anything)?

3. Is there a point to prototype functions? Why wouldn't the functions just be declared in advance?

Thanks again guys, your really helping this go much less painfully than last time I tried to learn lol.

I already answered 1 and 2; as for 3, the only reason I can think of is lazyness. I suppose it's also a (weak and unreliable) way of not exporting functions.

Adron

Quote from: j0k3r on July 04, 2003, 01:04 PM
More questions... (sorry)

1. Is "int main()" where the program is? And is it a functoin?

2. What is the point of "return 0;" in a program... Is it just there becase it is needed in a function? Is "return 0;" the same thing as saying "return void;" (telling it not to return anything)?

3. Is there a point to prototype functions? Why wouldn't the functions just be declared in advance?

Thanks again guys, your really helping this go much less painfully than last time I tried to learn lol.

1. main is the function that you write your program in. It will be called by the runtime library when your program is executed. It's sometimes given other names, but the principle is the same whether it's called main or winmain or wmain.

2. "return 0;" returns zero. If your function is declared to return a number, then you'll have to return some number. Main "has" to be declared to return an int, and so most people will return 0. This value can be checked by whoever started your program, so it's possible to return some kind of status code. See "if errorlevel" in batch files for example.

3. Prototyping functions is necessary for obvious reasons if two functions will be calling each other. Other than that, it allows you to gather all the prototypes in one spot, say a header file, that you can include anywhere, and yet move around the actual (possibly very big) functions to any file you like. Once you have a few thousand lines of code, you probably don't want to have it all in the same file or it'll become hard to navigate.