Velocity Reviews > I'm not understanding something

# I'm not understanding something

Guest
Posts: n/a

 02-23-2006
This is the problem that I have to solve :

Write a program to convert a temperature input in Celsius or Fahrenheit
to the other scale. Provide an appropriate prompt, and accept the
temperature input as a double, followed by a char representing the
scale (C or F). Perform the appropriate conversion and display the
original input and result. Write functions ftoc and ctof to do the
conversion and return the result to be displayed in main. The
conversion formulas are as follows, where C is a temperature in degrees
Celsius, and F the equivalent in Fahrenheit:

C = (5.0 / 9.0) * (F - 32)
F = ( (9.0 / 5.0) * C ) + 32

So basically if you enter a temp in Fahrenheit I need to convet it to
Celsius and vice versa.
The user input should be in the form of a double then the scale. For
example
32 F which stands for 32 degrees Fahrenheit. Here is my code so far.
Which compiles but will not execute

#include <stdio.h>
#include <stdlib.h>

double farTemp(double);
double centTemp(double);

int main()
{
double userTemp;
char scale;
char F,f,C,c;

printf("Enter Temp & Scale (F Or C ) ");
scanf("%lf%lf", &userTemp,&scale);

if(scale==f || scale== F)
centTemp(userTemp);

printf("%f", userTemp);

system("PAUSE");
return 0;
}

double CentTemp(double a)

{

return (5.0/9.0)*a -32 ;
}

Antonio Contreras
Guest
Posts: n/a

 02-23-2006

> This is the problem that I have to solve :
>
> Write a program to convert a temperature input in Celsius or Fahrenheit
> to the other scale. Provide an appropriate prompt, and accept the
> temperature input as a double, followed by a char representing the
> scale (C or F). Perform the appropriate conversion and display the
> original input and result. Write functions ftoc and ctof to do the
> conversion and return the result to be displayed in main. The
> conversion formulas are as follows, where C is a temperature in degrees
> Celsius, and F the equivalent in Fahrenheit:
>
> C = (5.0 / 9.0) * (F - 32)
> F = ( (9.0 / 5.0) * C ) + 32
>
> So basically if you enter a temp in Fahrenheit I need to convet it to
> Celsius and vice versa.
> The user input should be in the form of a double then the scale. For
> example
> 32 F which stands for 32 degrees Fahrenheit. Here is my code so far.
> Which compiles but will not execute
>
>
> #include <stdio.h>
> #include <stdlib.h>

> double farTemp(double);
> double centTemp(double);

Your specification clearly states that those functions should be called
ftoc and ctof. BTW the names your specification provides are much more
explicit than your choice. In case you didn't notice, ftoc stands for
"fahrenheit to celsius" and ctof for "celsius to fahrenheit". farTemp
gives the idea that it returns a fahrenheit value, but it doesn't give
a clue about what the argument may mean. Learn to name your functions
and variables in a explicit way. Choosing obfuscated identifiers to
save a few keystrokes can make debugging your code later twice as
difficult.

> int main()
> {
> double userTemp;
> char scale;
> char F,f,C,c;

Here you have declared a double that will hold the temperature provided
by the user, another one that will hold the scale specified by the
user... and four variables with no clear porpouse. It's a good idea to
intended use for the varible or the logic/computation/algorithm
implemented by the function.

On a side note, you have not declared a variable for holding your
computation. You could make without one, but I would add the following
line:

double computedTemp;

> printf("Enter Temp & Scale (F Or C ) ");

Notice that if you don't send a '\n' to the output there's no guarantee
that the text you sent will appear on the screen. So it may happen that
when the program executes the user is presented with a blank screen
while the program is awaiting for the user to input data. Modify the
above line as follows:

printf("Enter Temp & Scale (F Or C )\n");

> scanf("%lf%lf", &userTemp,&scale);

scanf is dangerous and should be avoided at all costs, but since this
is clearly a toy program we'll stick with it. Be warned however that
the fgets/sscanf combo is much safer.

The above line invokes undefined behaviour. You have specified in the
format string that you want to read 2 doubles, but you provide a
pointer to a float and a pointer to char. Your specification states
that you should read a char, so the obvious fix to this is changing the
format string to actually tell scanf that you want to read a char:

scanf("%lf %c", &userTemp, &scale);

Finally, you should be aware that scanf may fail parsing its arguments.
Imagine what will happen if the user inputs something like: "I can't
think of one temperature right now!"
scanf returns the number of arguments that where succesfully parsed and
assigned. Check that it's 2. In a toy program like this one, your best
option would be to exit with an error message. In real software you'll
have to consume the line from stdin (scanf will leave things in it) and
prompt the user again for correct data.

Furthermore, you should make sure that the data provided by the user
makes sense. How is your program to behave if the user prompts 'R' as
the temperature scale?

> if(scale==f || scale== F)

Here you are comparing the value of the variable scale with the value
of the variable f, and with the value of the variable F. Note that
neither f nor F have been initialized to a known value. Accessing an
unitiliazed variable invokes undefined behaviour.

I suspect that the check you really wanted to make is: (scale == 'f' ||
scale == 'F')

> centTemp(userTemp);

This line computes the celsius temperature and then thows it away. You
really want to store it somewhere.

I see no check for the selected scale beeing celsius, and no call to
the ctof function.

> printf("%f", userTemp);

This will print the value entered by the user. You don't want that.
Specifying the temperature scale in the output might be a good idea
too.

>
> system("PAUSE");

Implementation specific, and mostly unneeded if you use your program
from a CLI.

> return 0;
> }
>
> double CentTemp(double a)
>
> {
>
> return (5.0/9.0)*a -32 ;

Some spacing here would make this expression more readable.
> }

You should write a function to perform the inverse computation.

HTH

Guest
Posts: n/a

 02-23-2006
WoW, you really pointed out a lot of errors to me. Thank you. I'm going
to work on correcting them and then I'll post my updated code.

Pedro Graca
Guest
Posts: n/a

 02-23-2006
> This is the problem that I have to solve :

[snip: conversion from Fahrenheit to Celsius]

> Here is my code so far.
> Which compiles but will not execute

It didn't compile for me (*).
I'm a newbie C programmer, but (I hope) nothing is wrong with my

> #include <stdio.h>
> #include <stdlib.h>
>
> double farTemp(double);
> double centTemp(double);

Later, in main() you call centTemp -- but you never define it; you
define another function called `CentTemp'.

[some blank lines removed from the code]

> int main()
> {
> double userTemp;
> char scale;
> char F,f,C,c;
>
> printf("Enter Temp & Scale (F Or C ) ");

printf() should have a terminating '\n'

> scanf("%lf%lf", &userTemp,&scale);

Read two doubles into a double and a char?
My compiler issued a warning at this line.

Also scanf() is a bad choice to get user input.

> if(scale==f || scale== F)

Compare scale to two uninitialized variables?
.... I think you mean something else.

> centTemp(userTemp);

Call the (inexistent) centTemp function and discard the results.

> printf("%f", userTemp);
>
> system("PAUSE");
> return 0;
> }
>
> double CentTemp(double a)
> {
> return (5.0/9.0)*a -32 ;

This calculation does not calculate the same thing as the calculation

> }

(*) "It didn't compile for me" -- that was a lie.
Actually it compiled but didn't link.

--

Guest
Posts: n/a

 02-23-2006
Here is my updated code, which will compile now but it wont let me
enter a scale.
I know the code is incomplete I'm just trying to get it to work with
the celcius to fahrenheit
scale then i'll go back in and put in the fahrenheit to celcius. What's
wrong with waht I have now?

Guest
Posts: n/a

 02-23-2006
Here is my updated code, which will compile now but it wont let me
enter a scale.
I know the code is incomplete I'm just trying to get it to work with
the celcius to fahrenheit
scale then i'll go back in and put in the fahrenheit to celcius. What's
wrong with waht I have now?

#include <stdio.h>
#include <stdlib.h>

double ftoc(double);
double ctof(double);

int main()
{
double userTemp; // user inputed temp
char scale; // what scale the temp is in
char F,f,C,c; // storage of the char for the different temps
double finalOutput; // output of the conversion

printf("Enter Temp & Scale (F Or C ) \n");
scanf("%lf%c", &userTemp,&scale);

if(scale == 'f' || scale == 'F')
{
finalOutput = ftoc(userTemp);
printf(" %d ", finalOutput);
}

system("PAUSE");
return 0;
}

double ftoc(double a)

{

return (5.0/9.0)*a -32 ;
}

double ctof(double b)

{
return ( (9.0 / 5.0) * b) + 32;
}

Antonio Contreras
Guest
Posts: n/a

 02-23-2006
> Here is my updated code, which will compile now but it wont let me
> enter a scale.

> I know the code is incomplete I'm just trying to get it to work with
> the celcius to fahrenheit
> scale then i'll go back in and put in the fahrenheit to celcius. What's
> wrong with waht I have now?
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> double ftoc(double);
> double ctof(double);
>
> int main()
> {
> double userTemp; // user inputed temp
> char scale; // what scale the temp is in
> char F,f,C,c; // storage of the char for the different temps

You don't use these variables. There's no need to declared them.

On a side note, // style comments are not a good idea when posting code
to usenet as they don't survive line wrapping. The /* */ style is
prefered.

> double finalOutput; // output of the conversion
>
> printf("Enter Temp & Scale (F Or C ) \n");
> scanf("%lf%c", &userTemp,&scale);

You still don't check the return value of scanf. Did it parse both
fields and assign a value to both variables. Check it, print it out and
you'll get some idea of what's going on. Trying to debug code
blindfolded is not a good idea.

If scanf parsed the two fields, print the values of userTemp and scale.
That might give you some hints.

> if(scale == 'f' || scale == 'F')
> {
> finalOutput = ftoc(userTemp);
> printf(" %d ", finalOutput);

finalOutput is a double, buth since you provided a %d specifier, printf
expects an integer. You have undefined behaviour right there. Check you
documentation for the proper format specifier for printing doubles.
Hint: it is very similar to the format you used for parsing a double
with scanf.

> }

<snipped some blank lines>

>
> system("PAUSE");
> return 0;
> }
>
> double ftoc(double a)
>
> {
>
> return (5.0/9.0)*a -32 ;
> }

As Pedro Graca pointed and I failed to notice in my previous post, this

What I said in my other post about variable and function names is also
true for parameters. Their name should be descriptive. a doesn't give
any idea of what it is suppossed to be. IMO it would be much better if
you declared the function like:

double ftoc (double fahrenheit)

OR

double ftoc (double fahr)

if you really want to save those key-strokes.

>
>
> double ctof(double b)
>
> {
> return ( (9.0 / 5.0) * b) + 32;
> }

Guest
Posts: n/a

 02-23-2006
I Think I have it now. This is working for me. I really appreciate all
the help

#include <stdio.h>
#include <stdlib.h>

double ftoc(double);
double ctof(double);

int main()
{
double userTemp= 0; // user inputed temp
char scale; // what scale the temp is in
char F,f,C,c; // storage of the char for the different temps
double finalOutput; // output of the conversion

printf("Enter Temp & Scale (F Or C ) \n");
scanf("%lf %c", &userTemp,&scale);

if(scale == 'f' || scale == 'F')
{
finalOutput = ftoc(userTemp);
printf(" %lf \n", finalOutput);
}

else
{if(scale=='C' || scale=='c')
finalOutput= ctof(userTemp);
printf("%lf\n", finalOutput);
}

system("PAUSE");
return 0;
}

double ftoc(double a)

{

return (5.0/9.0)*a -32 ;
}

double ctof(double b)

{
return ( (9.0 / 5.0) * b) + 32;
}

pete
Guest
Posts: n/a

 02-23-2006
>
> I Think I have it now.

> double ftoc(double a)
>
> {
>
> return (5.0/9.0)*a -32 ;
> }

(5.0 / 9.0) * (a - 32)

--
pete

Keith Thompson
Guest
Posts: n/a

 02-24-2006
Pedro Graca <(E-Mail Removed)> writes:

[...]
>> printf("Enter Temp & Scale (F Or C ) ");

>
> printf() should have a terminating '\n'

It's reasonable not to have a terminating '\n' on an interactive
prompt, but you need to call fflush(stdout) to make sure the prompt
actually appears.

You do need a trailing '\n' on your program's final output; it's
implementation-defined whether the last line of a text stream (such as
stdout) requires a new-line.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.