Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Fraction class
Index -> Programming, C++ -> C++ Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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:

code:
#include <iostream>
#include "Fraction.h"

int main()
{
   Fraction a(1, 3), b(1, 2);

   std::cout << a + b << std::endl;

   return 0;
}



Fraction.tar.gz
 Description:

Download
 Filename:  Fraction.tar.gz
 Filesize:  2.11 KB
 Downloaded:  324 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
cimnik029




PostPosted: Wed Mar 30, 2005 8:46 pm   Post subject: (No subject)

does it reduce?
wtd




PostPosted: Wed Mar 30, 2005 8:55 pm   Post subject: (No subject)

If I recall, there's a "simplify" function in there, yes.
jamonathin




PostPosted: Wed Apr 13, 2005 8:02 am   Post subject: (No 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++ )
Martin




PostPosted: Wed Apr 13, 2005 8:04 am   Post subject: (No subject)

What are you using to try to run it?
jamonathin




PostPosted: Wed Apr 13, 2005 8:06 am   Post subject: (No subject)

I'm using CTRL+F9 to compile, and CTRL+F10 to run.
Martin




PostPosted: Wed Apr 13, 2005 8:10 am   Post subject: (No subject)

I mean, what program are you using? Visual Studio?

If so, you should run the program with F5, or click Debug -> Start
jamonathin




PostPosted: Wed Apr 13, 2005 8:12 am   Post subject: (No subject)

Dev-C++ 4.9.9.2, it's all I could find at the moment for free.
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Wed Apr 13, 2005 8:16 am   Post subject: (No 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):

code:
int buff;
cin >> buff;
jamonathin




PostPosted: Wed Apr 13, 2005 8:26 am   Post subject: (No 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:
code:
#include <iostream>
using namespace std;

void main()
{
     cout << "Hello World!\n";
}

it says
Quote:
5 \\KCI-FS\travmacv\CPU Science - C++\- Open Here.cpp `main' must return `int'
Martin




PostPosted: Wed Apr 13, 2005 8:36 am   Post subject: (No 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.
jamonathin




PostPosted: Wed Apr 13, 2005 8:45 am   Post subject: (No subject)

ahh ok, thanks, ( took me a couple tries ). But now when I put the
code:
int buff;
cin >> buff;

before the last }, it doesn't hold up for me, or say Hello world. Confused
[ I feel like such a newb ]
Martin




PostPosted: Wed Apr 13, 2005 9:01 am   Post subject: (No subject)

code:
#include <iostream>
using namespace std;

int main ()
{
     cout << "Hello World\n!";
     int buff;
     cin >> buff;
     return 0;
}
jamonathin




PostPosted: Wed Apr 13, 2005 9:03 am   Post subject: (No subject)

you're a bloody genious Head Bang thanks for your help martin.
Martin




PostPosted: Wed Apr 13, 2005 9:15 am   Post subject: (No 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:

code:
#include <iostream>

int main ()
{
     std::cout << "Hello World\n!";
     int buff;
     std::cin >> buff;
     return 0;
}


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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 25 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: