Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Temperature Conversion Program - Still a Rookie (http://www.velocityreviews.com/forums/t495448-temperature-conversion-program-still-a-rookie.html)

 AsheeG87@msn.com 03-31-2007 09:22 PM

Temperature Conversion Program - Still a Rookie

Hey Everyone~
I'm still a C++ Rookie so please bear with me on this.
I'm doing a temperature conversion program with prototype functions.
Basicly, I was wondering if some of you would take a look at my code
and critique it for me. I'm mostly concerned with how prototype
functions work and if I designed them correctly in my code.
Your participation would be greatly appreciated!

My Code:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

double FahrenheittoCelsius(double)= 0.0; //Function Prototype
double CelsiustoFahrenheit(double)= 0.0;

int _tmain(int argc, _TCHAR* argv[])
{
double tempFahrenheit, tempCelsius;
double temp;
char f;
char c;

cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >> temp;
cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
if (temp == f)
{
c = FahrenheittoCelsius;
cout << f " degrees Fahrenheit is" c " degrees celsius ";
cout << " Continue? ";

else
f = CelsiustoFahrenheit;
cout << c " degrees Celsius is " f " degrees Fahrenheit ";
cout << " Continue? ";

}

return temp;
}
double FahrenheitoCelsius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahrenheit (double c)
{
return (9/5.0 * c) + 32;
}

 Ian Collins 03-31-2007 10:33 PM

Re: Temperature Conversion Program - Still a Rookie

AsheeG87@msn.com wrote:
> Hey Everyone~
> I'm still a C++ Rookie so please bear with me on this.
> I'm doing a temperature conversion program with prototype functions.
> Basicly, I was wondering if some of you would take a look at my code
> and critique it for me. I'm mostly concerned with how prototype
> functions work and if I designed them correctly in my code.
> Your participation would be greatly appreciated!
>
> My Code:
>

I assume you haven't tried to compile this!

Some hints:

> #include "stdafx.h"

> #include <iostream>
> #include <cstdlib>
>
> double FahrenheittoCelsius(double)= 0.0; //Function Prototype
> double CelsiustoFahrenheit(double)= 0.0;
>

These are syntax errors, not prototypes. Loose the = 0.0.

> int _tmain(int argc, _TCHAR* argv[])

That's _TCHAR? Use

int main( int argc, char* argv[] )

> {
> double tempFahrenheit, tempCelsius;
> double temp;
> char f;
> char c;
>
>
> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> temp;
> cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
> if (temp == f)

Where is f assigned?

> {
> c = FahrenheittoCelsius;

Here you attempt to assign a function to a char, which is a very strange
thing to do! Did you mean to write

c = FahrenheittoCelsius( temp );

> cout << f " degrees Fahrenheit is" c " degrees celsius ";
> cout << " Continue? ";
>

missing }
> else

missing {
> f = CelsiustoFahrenheit;
> cout << c " degrees Celsius is " f " degrees Fahrenheit ";
> cout << " Continue? ";
>
> }
>
>
> return temp;
> }
> double FahrenheitoCelsius (double f)
> {
> return (f - 32) * (5.0/9);
> }
> double CelsiustoFahrenheit (double c)
> {
> return (9/5.0 * c) + 32;
> }
>

--
Ian Collins.

 Jim Langston 04-01-2007 01:30 AM

Re: Temperature Conversion Program - Still a Rookie

<AsheeG87@msn.com> wrote in message
> Hey Everyone~
> I'm still a C++ Rookie so please bear with me on this.
> I'm doing a temperature conversion program with prototype functions.
> Basicly, I was wondering if some of you would take a look at my code
> and critique it for me. I'm mostly concerned with how prototype
> functions work and if I designed them correctly in my code.
> Your participation would be greatly appreciated!
>
> My Code:
>
> #include "stdafx.h"
> #include <iostream>
> #include <cstdlib>
>
> double FahrenheittoCelsius(double)= 0.0; //Function Prototype

This is not a pure virtual method. It is a function prototype. It should
look like this:
double FarenheitToCelcius(double);

> double CelsiustoFahrenheit(double)= 0.0;

double CelciusToFarenheit(double);

> int _tmain(int argc, _TCHAR* argv[])

This is non standard, becuase you did a windows project and are using
standard headers. That is frowned upon in this newsgroup.

int main()

> {
> double tempFahrenheit, tempCelsius;

Better to call these Farenheit and Celcius so you don't confuse yourself.

double Farenheit, Celcious;

> double temp;

> char f;
> char c;

These are not the characters 'f' and 'c' but variables of type char that are
called f and c. You can get rid of these.

> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> temp;
> cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
> if (temp == f)

You are asking if temp == f. temp is a double. f is a char. I think you
actually wanted to ask the user but forgot to do it.

char torf;
cin >> torf;

if ( torf == 'f' )

> {
> c = FahrenheittoCelsius;

You need to call a function there. Your prototype is:
double FarenheitToCelcius(double);
so it would be:

Celcius = FarenheitToCelcius( temp );

> cout << f " degrees Fahrenheit is" c " degrees celsius ";

That ain't going to work. It should be:
cout << Farenheit << " degrees Fahrenheit is" << Celcius << " degrees
celsius ";

> cout << " Continue? ";
>
> else
> f = CelsiustoFahrenheit;
> cout << c " degrees Celsius is " f " degrees Fahrenheit ";

See comments for converting from FarenheitToCelcius and fix this to reflect.

> cout << " Continue? ";
>
> }
>
>
> return temp;
> }
> double FahrenheitoCelsius (double f)
> {
> return (f - 32) * (5.0/9);
> }
> double CelsiustoFahrenheit (double c)
> {
> return (9/5.0 * c) + 32;
> }

Okay, try all tis, then actually try to compile it. You'll get compilation
errors variables not found, etc.. Fix the errors. If you still get stuck
post again with your new fixed code.

 AsheeG87@msn.com 04-01-2007 05:22 AM

Re: Temperature Conversion Program - Still a Rookie

You are a lot of help! And...I feel dumb about some of the mistakes
that I made. I have no clue where I came up with that. But, I'm still
learning. So, here we go. I am having a problem debugging. It won't
execute the program. So, I can build and see the errors that come up
but it won't execute. What am I missing?

Here is my *NEW* code:

// Temperature.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

double FahrenheittoCelsius(double); //Function Prototype
double CelsiustoFahrenheit(double);

int main()
{
double tempFahrenheit, tempCelsius;
double temp;
char corf;

cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >> corf;

if (corf == 'f')
{
tempCelsius = FahrenheittoCelsius( temp );
cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
<< " degrees celsius ";
cout << " Continue? ";
if
tempFahrenheit = CelsiustoFahrenheit(temp);
cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
degrees Fahrenheit";
cout << " Continue? ";
}

return 0;
}
double FahrenheittoCelsius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahrenheit (double c)
{
return (9/5.0 * c) + 32;
}

 Ian Collins 04-01-2007 07:14 AM

Re: Temperature Conversion Program - Still a Rookie

AsheeG87@msn.com wrote:
> You are a lot of help! And...I feel dumb about some of the mistakes
> that I made. I have no clue where I came up with that. But, I'm still
> learning. So, here we go. I am having a problem debugging. It won't
> execute the program. So, I can build and see the errors that come up
> but it won't execute. What am I missing?
>
> Here is my *NEW* code:
>

Which *still* shouldn't compile!

> // Temperature.cpp : Defines the entry point for the console
> application.
> //
> #include "stdafx.h"

Loose this.

> #include <iostream>
> #include <cstdlib>
> using namespace std;
>
> double FahrenheittoCelsius(double); //Function Prototype
> double CelsiustoFahrenheit(double);
>
> int main()
> {
> double tempFahrenheit, tempCelsius;
> double temp;
> char corf;
>
>
> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> corf;
>
> if (corf == 'f')
> {
> tempCelsius = FahrenheittoCelsius( temp );
> cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
> << " degrees celsius ";
> cout << " Continue? ";

missing }
> if

missing condition and {

--
Ian Collins.

 ajk 04-01-2007 10:25 AM

Re: Temperature Conversion Program - Still a Rookie

On 31 Mar 2007 14:22:44 -0700, AsheeG87@msn.com wrote:

>#include "stdafx.h"
>#include <iostream>
>#include <cstdlib>
>
>double FahrenheittoCelsius(double)= 0.0; //Function Prototype
>double CelsiustoFahrenheit(double)= 0.0;
>

double FahrenheittoCelsius(double arg);
double CelsiustoFahrenheit(double arg);

>int _tmain(int argc, _TCHAR* argv[])
>{

fine, visual studio will set correct character type depending on
unicode or multibyte but for now use char instead, since you are
learning.

> double tempFahrenheit, tempCelsius;
> double temp;
> char f;
> char c;
>

preferably initialize them to some invalid - for your purposes -
value.

>
> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> temp;

ok

> cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
> if (temp == f)
> {

temp is of type double and f is of type char - what are you doing
here? looks like you have misstakenly taken then variable name f as
the variable value 'f'.

const char celsius = 'c';
const char fahrenheit = 'f';
char choice;

cin >> choice;
if ( choice == celsius )
{
}

> c = FahrenheittoCelsius;
> cout << f " degrees Fahrenheit is" c " degrees celsius ";
> cout << " Continue? ";
>
> else
> f = CelsiustoFahrenheit;
> cout << c " degrees Celsius is " f " degrees Fahrenheit ";
> cout << " Continue? ";
>
> }
>
>
> return temp;
>}
>double FahrenheitoCelsius (double f)
>{
> return (f - 32) * (5.0/9);
>}
>double CelsiustoFahrenheit (double c)
>{
> return (9/5.0 * c) + 32;
>}

hth/ajk

 ajk 04-01-2007 11:09 AM

Re: Temperature Conversion Program - Still a Rookie

On 31 Mar 2007 22:22:48 -0700, AsheeG87@msn.com wrote:

>// Temperature.cpp : Defines the entry point for the console
>application.
>//
>#include "stdafx.h"
>#include <iostream>
>#include <cstdlib>
>using namespace std;
>
>double FahrenheittoCelsius(double); //Function Prototype
>double CelsiustoFahrenheit(double);
>
>int main()
>{
> double tempFahrenheit, tempCelsius;
> double temp;
> char corf;
>
>
> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> corf;
>
> if (corf == 'f')
> {
> tempCelsius = FahrenheittoCelsius( temp );

temp is undefined, it hasn't been set to a value

> cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
><< " degrees celsius ";
> cout << " Continue? ";
> if
> tempFahrenheit = CelsiustoFahrenheit(temp);
> cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
>degrees Fahrenheit";
> cout << " Continue? ";
> }
>
> return 0;
>}
>double FahrenheittoCelsius (double f)
>{
> return (f - 32) * (5.0/9);
>}
>double CelsiustoFahrenheit (double c)
>{
> return (9/5.0 * c) + 32;
>}

 Jim Langston 04-01-2007 01:56 PM

Re: Temperature Conversion Program - Still a Rookie

<AsheeG87@msn.com> wrote in message
> You are a lot of help! And...I feel dumb about some of the mistakes
> that I made. I have no clue where I came up with that. But, I'm still
> learning. So, here we go. I am having a problem debugging. It won't
> execute the program. So, I can build and see the errors that come up
> but it won't execute. What am I missing?
>
> Here is my *NEW* code:
>
> // Temperature.cpp : Defines the entry point for the console
> application.
> //
> #include "stdafx.h"

You don't need to include stdafx.h anymore, that was part of precompiled

> #include <iostream>
> #include <cstdlib>
> using namespace std;
>
> double FahrenheittoCelsius(double); //Function Prototype
> double CelsiustoFahrenheit(double);
>
> int main()
> {
> double tempFahrenheit, tempCelsius;
> double temp;
> char corf;
>
>
> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> corf;

You send the message to the console for the user to enter the Temperature,
but then you have them input if it's celsius or centigrade.

This should be:

cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >> temp;
cout << "Celcius or Farenheit? ";
cin >> corf;

> if (corf == 'f')
> {
> tempCelsius = FahrenheittoCelsius( temp );
> cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
> << " degrees celsius ";
> cout << " Continue? ";
> if

Somehow you cut off this line. This should probably be:
if ( corf == 'c' )
{

> tempFahrenheit = CelsiustoFahrenheit(temp);
> cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
> degrees Fahrenheit";
> cout << " Continue? ";
> }
>
> return 0;
> }
> double FahrenheittoCelsius (double f)
> {
> return (f - 32) * (5.0/9);
> }
> double CelsiustoFahrenheit (double c)
> {
> return (9/5.0 * c) + 32;
> }

 Default User 04-01-2007 06:40 PM

Re: Temperature Conversion Program - Still a Rookie

Jim Langston wrote:

> You send the message to the console for the user to enter the
> Temperature, but then you have them input if it's celsius or
>
> This should be:
>
> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> temp;
> cout << "Celcius or Farenheit? ";
> cin >> corf;
>
> > if (corf == 'f')

This isn't a very robust design. There are all kinds of unused
characters left hanging around. I'd recommend reading in the entire
line and examining the first character.

Brian

 AsheeG87@msn.com 04-01-2007 08:41 PM

Re: Temperature Conversion Program - Still a Rookie

Alright guys....
I agree with you that stdafx.h does not need to be included but it
won't compile without it.
Ok....so,I started playing around with it and I got the program to
finally compile and it is working fairly ok.
The program will exit when I enter a value because temp,
tempFahrenheit, and tempCelsius have not been intialized. When I set
it to 0 I still get the same problem. Any ideas?
Oh by the way....here is the updated code from this afternoon.

// Temperature.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

double FahrenheittoCelsius(double); //Function Prototype
double CelsiustoFahrenheit(double);

int main()
{
double tempFahrenheit, tempCelsius;
double temp;
char corf;

cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >> temp;
cout << " Fahrenheit or Celsius? ";
cin >> corf;

if (corf == 'f')
{
tempCelsius = FahrenheittoCelsius( temp );
cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
<< " degrees celsius ";
cout << " Continue? ";

}
if (corf == 'c')
{
tempFahrenheit = CelsiustoFahrenheit(temp);
cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
degrees Fahrenheit";
cout << " Continue? ";

}

return temp;
}
double FahrenheittoCelsius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahrenheit (double c)
{
return (9/5.0 * c) + 32;
}

All times are GMT. The time now is 10:30 PM.