Author | Message | Time |
---|---|---|
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 ... [/quote] 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. | July 2, 2003, 7:36 PM |
j0k3r | Oh yes, any free compilers you know of would be nice, especially the easy to install ones. | July 2, 2003, 9:01 PM |
TheMinistered | Yes, they are wrong. If it were 100 / 10 instead, then they would be right, however the remainder is 0. | July 2, 2003, 9:30 PM |
Eibro | [quote author=j0k3r link=board=5;threadid=1783;start=0#msg13654 date=1057179670] Oh yes, any free compilers you know of would be nice, especially the easy to install ones. [/quote]Check http://www.bloodshed.net | July 2, 2003, 9:45 PM |
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. | July 2, 2003, 10:15 PM |
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. | July 3, 2003, 10:40 PM |
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? | July 3, 2003, 11:34 PM |
Camel | [quote author=j0k3r link=board=5;threadid=1783;start=0#msg13766 date=1057275265] What does "return 0;" do? What are the seperate "#include <so-and-so.h>" statements and what are they used for? [/quote] "return 0;" makes a function return zero. Most functions you'll find will return a value (except the ones declared as 'void'). Example: [code]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. }[/code] #include is used to 'include' a header (.h) file. Generally, a header file is used to declare functions in seperate .c/.cpp/.dll files. [code]<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(); }[/code] 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. | July 3, 2003, 11:56 PM |
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? | July 4, 2003, 1:21 AM |
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. | July 4, 2003, 1:50 AM |
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 | July 4, 2003, 6:23 AM |
Adron | [quote author=j0k3r link=board=5;threadid=1783;start=0#msg13784 date=1057281673] Is the only way of knowing what's in them to open them and read? [/quote] 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. | July 4, 2003, 10:33 AM |
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. | July 4, 2003, 6:04 PM |
Camel | [quote author=j0k3r link=board=5;threadid=1783;start=0#msg13840 date=1057341898] 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. [/quote] 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. | July 4, 2003, 7:00 PM |
Adron | [quote author=j0k3r link=board=5;threadid=1783;start=0#msg13840 date=1057341898] 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. [/quote] 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. | July 5, 2003, 10:34 AM |
j0k3r | Thank you Adron. | July 5, 2003, 12:10 PM |
JoeCool | if its "C++ For Dummies" it doesnt really mean its for idiots.. it just means it explains it really thuroly.. for people that wont get it in a normal book that just started C++ | July 9, 2003, 6:13 PM |
j0k3r | Wow man this is getting harder... 1. Someone help me with hex... How do you convert hex to numbers (multiply the digits by 16 and add the letters?) and numbers to hex (don't get this one at all)? I sort of have it figured out by doing it through binary but I'd rather know how to do it normally. 2. Are pointers used very often in C++ (how long do I have to make my brain hurt to understand this enough)? 3. If it won't take long, can someone enlighten me on memory location things (0x123456, etc)? | July 20, 2003, 12:44 AM |
Eibro | [quote author=j0k3r link=board=5;threadid=1783;start=15#msg15344 date=1058661865] Wow man this is getting harder... 1. Someone help me with hex... How do you convert hex to numbers (multiply the digits by 16 and add the letters?) and numbers to hex (don't get this one at all)? I sort of have it figured out by doing it through binary but I'd rather know how to do it normally. 2. Are pointers used very often in C++ (how long do I have to make my brain hurt to understand this enough)? 3. If it won't take long, can someone enlighten me on memory location things (0x123456, etc)? [/quote]1) Do you mean convert hexadecimal to decimal? You really don't need to do this much on the fly (you could use the windows calculator, too) but, here: http://www.permadi.com/tutorial/numHexToDec/ 2) Yes, they are. I know how you feel; I had no understanding of computer architecture/memory addressing when I started C++, so it was hard for me to understand as well. If you don't see a use for them right now, I wouldn't worry about it too much. I'm sure you'll come across them sooner or later. 3) This is a pretty broad topic. What do you want to know? | July 20, 2003, 12:55 AM |
j0k3r | Thanks for the link Eibro it cleared up everything, and I guess I'll just read through pointers and attempt to understand but not spend much time for now. As for the memory thing... 1. What does 0x have to do with it? Is it just there to tell you it's referring to memory? Is there such thing as 1x? 2. Is the range affected by how much RAM you have (I'm going out on a limb here assuming it pertains to RAM)? | July 20, 2003, 1:48 AM |
Eibro | 1) If you ever see 0x infront of a number, it doesn't necessarily mean it's referring to a memory address. It simply means the number is expressed in hexadecimal. You'll also see hexadecimal numbers written with an upper or lowercase h prepended or appended to the number (although not in C/C++) 0xBADF00D == HBADF00D == BADF00Dh it's all just another way of saying the same thing. Also, there's no such thing as 1x :) 2) Now that that's cleared up, it adds a little ambiguity to the next question; the range of what? | July 20, 2003, 2:10 AM |
iago | The operating system allocates you ram for your program. When you refer to an address in the program (say, 0xbadf00d), it's generally referring to somewhere in your ram, but there's no way of knowing where. The operating system takes care of that for you so you never know where it is. | July 20, 2003, 2:29 AM |
Camel | [quote author=iago link=board=5;threadid=1783;start=15#msg15353 date=1058668169]but there's no way of knowing where.[/quote] There's no practical way of knowing where. ;D Even if you did know where the data was, it would be fairly useless information as memory can be manipulated by other, much simpler, means through the operating system. | July 20, 2003, 8:28 AM |
j0k3r | Alright that cleared alot too, thanks. | July 20, 2003, 10:29 AM |