Help with Classes

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

Join Date: Jul 2004
Posts: 3
Reputation: biel_andrada is an unknown quantity at this point 
Solved Threads: 0
biel_andrada biel_andrada is offline Offline
Newbie Poster

Help with Classes

 
0
  #1
Aug 20th, 2004
Hi!
Thanks for the help regarding vectors It really helped me.

Now, I have a problem regarding classes. I do not know the problem in this program

#include <iostream.h>
#include <ctype.h> //islower(), toupper()
#include <stdlib.h> //exit( )
class Temp
{
public:
Temp( );
Temp(double initDeg, char initScale);
double Degrees();
char Scale();
Temp Fahrenheit( );
Temp Celsius( ); //needed to work on
Temp Kelvin( ); //needed to work on
void Read( );
void Print( );
private:
double myDegree;
char myScale;
};


Temp Temp :: Fahrenheit( )
{//converting to Fahrenheit
switch(myScale)
{
case ‘F’: return Temp(myDegrees, ‘F’);
case ‘C’: return Temp(myDegrees*1.8+32.0, ‘F’);
case ‘K’: return Temp(myDegrees-273.15, ‘F’);
}
}

void main()
{
cout<<"This program shows the Fahrenheit, Celcius,"
<<" and Kelvin equivalents of a temperature.\n\n";

char response;
Temp theTemp; //construction

do
{ cout<<"Enter a temperature(e.g., 98.6 F): ";
cin>>theTemp; //input
cout<<"-->"
<<theTemp.Fahrenheit()
<<" = "
<<theTemp.Celcius()
<<" = "
<<theTemp.Kelvin()
<<endl;
cout<<"\nDo you want to have more temperature conversion?";
cin>>response;
}while(response=='Y' || response=='y');

}

Please help me! Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Help with Classes

 
0
  #2
Aug 20th, 2004
Can you give us a hint as to where to look, what you are seeing, how you know it doesn't work? Does it compile? Run? Give unexpected results?

We might be able to help if we know what we're looking for.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 72
Reputation: gusano79 is on a distinguished road 
Solved Threads: 5
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: Help with Classes

 
0
  #3
Aug 20th, 2004
Well, here's an easy one--you've spelled a method two different ways (the first is correct):

Originally Posted by biel_andrada
...
Temp Celsius( ); //needed to work on
...
<<theTemp.Celcius()
...
Also, it won't compile until you actually define a body for that method like you did for Temp::Fahrenheit.

Hm, also noticed you have both myDegree and myDegrees in your code; I bet you don't want that either.

Unless you've snipped out lots of code, it looks like you haven't actually implemented half of the class members... including the constructor that actually accepts an argument and (one would imagine) initializes myDegree. Without that, it'll always be zero.

And... this looks suspicious:

Originally Posted by biel_andrada
...
cin>>theTemp; //input
...
There's no way for the compiler to know that you want a double-precision value to be read... you need a double somewhere to hold the value.

That should get you started.
--sg
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC