creating a class and declaring objects

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 4
Reputation: Guppy25 is an unknown quantity at this point 
Solved Threads: 0
Guppy25 Guppy25 is offline Offline
Newbie Poster

creating a class and declaring objects

 
0
  #1
Jan 15th, 2005
I've created a class Fraction and am trying to use it in a main () but keep getting an error in my compiler that says "main must return int". My teacher says it's fine but i need to compile and run so i know that it works. Any suggestions? Thank you, jennie
#include <iostream.h>


class Fraction
{
public:
int num, den;
};

void main()
{
Fraction oneFraction;
float decimal = oneFraction.num/oneFraction.den;

cout <<"Please enter a numerator "<<endl;
cin >>oneFraction.num;
cout <<"The numerator is "<<oneFraction.num<<endl;

cout <<"Please enter a denominator "<<endl;
cin >>oneFraction.den;

while (oneFraction.den == 0)
{
cout<<"Enter a number greater than zero."<<endl;
cin >>oneFraction.den;
}
cout <<"The denominator is "<<oneFraction.den<<endl;


cout <<"The fraction in decimal value is "<<decimal<<endl;
if(decimal>1)
cout<<"The decimal value is greater than one."<<endl;

getchar();

}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: creating a class and declaring objects

 
0
  #2
Jan 15th, 2005
try putting int main() ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: creating a class and declaring objects

 
0
  #3
Jan 15th, 2005
>compiler says "main must return int"
>teacher says it's fine
Your compiler is right and your teacher is wrong. main has never returned anything but int in C or C++. If a compiler allows it then it's a non-standard extension to the language and you can't expect your code to do anything meaningful on another compiler. The following definition for main will work everywhere without fail in any version of either C or C++:
  1. int main()
  2. {
  3. return 0;
  4. }
However, the recommended C style (that also works perfectly for C++) is
  1. int main(void)
  2. {
  3. return 0;
  4. }
to promote continuity with the way declarations are handled, and the recommended C++ style is
  1. int main()
  2. {
  3. }
because C++ will return 0 (for success) automagically, thus removing the only argument that idiotic void mainers constantly spout about returning 0 taking too much effort. The same feature is in the latest version of C, but because it isn't widely implemented yet, you should continue to work with the common subset of "old" C and "new" C.

If your teacher disagrees then direct him here and I'll be happy to explain in detail why he's stupid.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: creating a class and declaring objects

 
0
  #4
Jan 15th, 2005
lol can you not explain anyway? NAr I'm joking! But i cant belive i missed out return 0; didnt see that error....
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: creating a class and declaring objects

 
0
  #5
Jan 16th, 2005
When you get your program running your in for another surprise. Your program will say, for instance, that 3/5 = 0. The problem is that onefraction.num and onefraction.den are integrs and when you divide integers you just get their integer quotient. Something like this will work: decimal = (1.0*onefraction.num)/onefraction.den;, or you could use a cast.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC