Computer Science Canada Fraction class |
Author: | wtd [ Wed Mar 02, 2005 6:46 pm ] | ||
Post subject: | Fraction class | ||
When dealing with numbers, it's not uncommon for us to use floating point numbers. The problem, however, is that floating point numbers are inherently inaccurate. The solution? Create a fraction class that stores the numerator and denominator separately, and overloads a number of operators and functions so that fractions can be dealt with much the same as other numbers. Consider a simple example:
|
Author: | cimnik029 [ Wed Mar 30, 2005 8:46 pm ] |
Post subject: | |
does it reduce? |
Author: | wtd [ Wed Mar 30, 2005 8:55 pm ] |
Post subject: | |
If I recall, there's a "simplify" function in there, yes. |
Author: | jamonathin [ Wed Apr 13, 2005 8:02 am ] |
Post subject: | |
I'm totally new to this, and the only thing I've understood so far is the "Hello World" program, but why can't i run anything, not even this program, it says Source File Not Compiled. So then I go to compile it, and att he bottom it says: [Linker error] undefined reference to `WinMain@16' ld returned 1 exit status ( I'm using Dev-C++ ) |
Author: | Martin [ Wed Apr 13, 2005 8:04 am ] |
Post subject: | |
What are you using to try to run it? |
Author: | jamonathin [ Wed Apr 13, 2005 8:06 am ] |
Post subject: | |
I'm using CTRL+F9 to compile, and CTRL+F10 to run. |
Author: | Martin [ Wed Apr 13, 2005 8:10 am ] |
Post subject: | |
I mean, what program are you using? Visual Studio? If so, you should run the program with F5, or click Debug -> Start |
Author: | jamonathin [ Wed Apr 13, 2005 8:12 am ] |
Post subject: | |
Dev-C++ 4.9.9.2, it's all I could find at the moment for free. |
Author: | Martin [ Wed Apr 13, 2005 8:16 am ] | ||
Post subject: | |||
Okay, well, it seems like you're building it correctly. Does the DOS window just pop up and disappear? If so, everything went correctly and your program just finished its execution. Add the following line to the end of your code to make it wait for your input before it exits (above the return 0; below everything else):
|
Author: | jamonathin [ Wed Apr 13, 2005 8:26 am ] | ||
Post subject: | |||
Well, when I try this program, it says it must be compiled, so I compile it, and then I think it's done, I go to run it, and it says it must be compiled, and those error message I wrote earlier pop up. When I try this:
it says Quote: 5 \\KCI-FS\travmacv\CPU Science - C++\- Open Here.cpp `main' must return `int' |
Author: | Martin [ Wed Apr 13, 2005 8:36 am ] |
Post subject: | |
Ooh, good! main is a function. In C++, there are no procedures, instead, everything must return a value. If you don't want something to return a value, you have it return void. Your main function is the entry point to your code: that is, when your program is run, main is the first thing that is called. void main is technically acceptable, but it's considered to be very bad coding. Instead of void main, use int main. At the end of it, you'll want to make it 'return 0;' which basically tells the OS that everything went okay. |
Author: | jamonathin [ Wed Apr 13, 2005 8:45 am ] | ||
Post subject: | |||
ahh ok, thanks, ( took me a couple tries ). But now when I put the
before the last }, it doesn't hold up for me, or say Hello world. ![]() [ I feel like such a newb ] |
Author: | Martin [ Wed Apr 13, 2005 9:01 am ] | ||
Post subject: | |||
|
Author: | jamonathin [ Wed Apr 13, 2005 9:03 am ] |
Post subject: | |
you're a bloody genious ![]() |
Author: | Martin [ Wed Apr 13, 2005 9:15 am ] | ||
Post subject: | |||
No problem. One thing that always confused me when I started learning C++ was the use of the line 'using namespace std;' C++ has literally thousands of various functions, and, should you use a bunch of libraries, conflicts are going to occur when functions have the same name. 'using namespace std;' says 'use the standard (std) namespace' A namespace is used to battle the problem of conflicting names. With namespaces, functions belong to a collection; in the above case, they belong to the 'std' collection. The above code that I posted is the exact equivalent of this, without namespaces:
Note the std:: at the beginning of the function. This says 'use std's cout function.' The line 'using namespace std;' says 'whenever a function's not defined by the code, check to see if it's defined in std' Now I suggest we leave wtd's fraction class topic alone. Make a new topic if you need more help. |
Author: | Justin_ [ Thu Feb 02, 2006 5:32 pm ] |
Post subject: | |
Did you code this wtd? Looks to be pretty slick coding ![]() |
Author: | wtd [ Thu Feb 02, 2006 5:46 pm ] |
Post subject: | |
Yep, and thanks. ![]() |
Author: | Justin_ [ Thu Feb 02, 2006 11:10 pm ] |
Post subject: | |
might want to work on your commenting though :p btw, what do you do for a living wtd? And how old are you? |
Author: | wtd [ Thu Feb 02, 2006 11:22 pm ] |
Post subject: | |
What's wrong with my use of comments? I'm older than dirt, and am currently unemployed as required by Canadian law since I'm working through the immigration process. |
Author: | Hikaru79 [ Thu Feb 02, 2006 11:29 pm ] |
Post subject: | |
wtd wrote: What's wrong with my use of comments?
Um, I might be missing something, but ... there is no use of comments. |
Author: | wtd [ Thu Feb 02, 2006 11:31 pm ] |
Post subject: | |
Hikaru79 wrote: wtd wrote: What's wrong with my use of comments?
Um, I might be missing something, but ... there is no use of comments. Yes, I'm guessing that's what Justin_ is taking exception with. |
Author: | Justin_ [ Thu Feb 02, 2006 11:51 pm ] |
Post subject: | |
wtd wrote: What's wrong with my use of comments? That question stimulates a good laugh anyway ![]() wtd wrote: I'm older than dirt, and am currently unemployed as required by Canadian law since I'm working through the immigration process. I'm sorry to hear that. Where are you native to if not Canada? There's a good loophole in the immigration system, all you have to do is marry a Canadian citizen and your in the door. |
Author: | wtd [ Thu Feb 02, 2006 11:56 pm ] |
Post subject: | |
Justin_ wrote: wtd wrote: What's wrong with my use of comments? That question stimulates a good laugh anyway ![]() I don't use comments when my code is self-documenting. The code spoke for itself. I take it you find this concept strange. Justin_ wrote: wtd wrote: I'm older than dirt, and am currently unemployed as required by Canadian law since I'm working through the immigration process. I'm sorry to hear that. Where are you native to if not Canada? There's a good loophole in the immigration system, all you have to do is marry a Canadian citizen and your in the door. I'm originally from the US, and I am married to a Canadian citizen. The notion that doing so guarantees a person citizenship in Canada is a myth. It "gets your foot in the door", but nothing is certain. |
Author: | Justin_ [ Fri Feb 03, 2006 12:11 pm ] |
Post subject: | |
I knew you would suggest that your code was self documenting, which it is, but it is always nice to throw in a few comments on some parts of the code you may have had trouble with, or something to that extent. |
Author: | wtd [ Fri Feb 03, 2006 4:06 pm ] |
Post subject: | |
I didn't have trouble with any of it. It was a bit tedious, but not difficult. ![]() |