Re: I'm not understanding something



Several errors in your code. Read comments below.

RadiationX wrote:
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
add comments when declaring functions and variables explaining the
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

.



Relevant Pages

  • Re: Kanji for ishi (stone) can be pronounced koku
    ... degrees for ice, and human body temperature for 100. ... a bit below ideal temperature, ... In the original scale devised by Anders Celsius the boiling point of water at 1,000 millibars was defined as 0 degrees and the freezing point of water was defined as 100 degrees, exactly the reverse of the modern Celsius scale. ...
    (sci.lang.japan)
  • Re: Im not understanding something
    ... Write a program to convert a temperature input in Celsius or Fahrenheit ... to the other scale. ...
    (comp.lang.c)
  • Im not understanding something
    ... Write a program to convert a temperature input in Celsius or Fahrenheit ... to the other scale. ...
    (comp.lang.c)
  • Re: Units question for Americans
    ... > As a scale for giving weather results in, ... temperature and so is better for describing weather than Celcius. ... The zero point of Celsius is set at a significant discontinuity that of Fahrenheit is set at an insignificant discontinuity. ...
    (rec.arts.sf.written)
  • Re: Units question for Americans
    ... it is based upon a scale where zero is below the ... temperature and so is better for describing weather than Celcius. ... Even Herr Fahrenheit ... single centigrade experiences save as as rare occasion. ...
    (rec.arts.sf.written)