473,218 Members | 1,402 Online

# I'm not understanding something

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 ;
}

Feb 23 '06 #1
23 1979

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

Feb 23 '06 #2
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.

Feb 23 '06 #3
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.

--
Feb 23 '06 #4
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?

Feb 23 '06 #5
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;
}

Feb 23 '06 #6
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;
}

Feb 23 '06 #7
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;
}

Feb 23 '06 #8

I Think I have it now. double ftoc(double a)

{

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

(5.0 / 9.0) * (a - 32)

--
pete
Feb 24 '06 #9
Pedro Graca <he****@dodgeit.com> 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) ks***@mib.org <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.
Feb 24 '06 #10
Now i'm having problems again. The program is working but it keeps
outputting the same values over and over again.

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

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

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

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

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

else
{
if(scale=='C' || scale=='c')
{
finalOutput= ctof(userTemp);
printf("%.1lfC = %.1lfF \n",userTemp,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;
}

Feb 24 '06 #11

Now i'm having problems again. The program is working but it keeps
outputting the same values over and over again.

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

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

int main()
{
double userTemp; // user inputted temp
char scale; // what scale the temp is in
char F,f,C,c; // storage of the char for the different temps
The four variables are never used and can be omitted.
double finalOutput; // conversion output

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

Just a guess: should be %lf and not f, since userTemp is double...

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

regards
John
Feb 24 '06 #12
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
These variables are not used. They don't serve any porpouse. Remove
them.
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);
The correct format specifier for printing doubles is %f, not %lf. In
fact if you read your manual pages you'll see that the l length
modifier cannot be applied to the f conversion specifier.
}

else
{if(scale=='C' || scale=='c')
finalOutput= ctof(userTemp);
printf("%lf\n", finalOutput);
}
The logic of this case is faulty. Imagine that the user enters 'H' as
the temperature scale. The condition in the first if is not met so you
fall to this else. Now the condition of this if is not met so you don't
execute the call to ctof and don't assign to finalOutput. But in the
next line (which is outside the if statement) you use the value of the
yet unassigned finalOutput. This invokes undefined behaviour, and while
the most usual behaviour would be to print some random value, it is
also possible for other things to happen. In fact everything may happen
as far as the language is concerned.

I suspect that what you really want is:

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

On a side note, with my modification if the user enters a scale
temperature that doesn't match any of the conditions the program will
exit without warning the use why it is not doing anything. This is
rude. You should print some kind of message indicating that the data
entered was not correct and that your aborting execution.

<snip blanks>

I can't stop myself from wondering why you use so little horizontal
spacing while using lots of unnecessary vertical space...
system("PAUSE");
return 0;
}

double ftoc(double a)

{

return (5.0/9.0)*a -32 ;
}
As has been pointed to you at least three times by now the above
expression is not correct. Multiplication has a higher precedence than
substraction so the above expression parses as:

(5.0 / 9.0 * a) - 32

which is not what you want.

double ctof(double b)

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

Lots of unnecessary parentheses.

Feb 24 '06 #13
On 23 Feb 2006 13:42:59 -0800, "RadiationX" <he*********@gmail.com>
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);

int main()
{
double userTemp;
char scale;
char F,f,C,c;
printf("Enter Temp & Scale (F Or C ) ");
scanf("%lf%lf", &userTemp,&scale);
The second %lf says the input value will be a double. The
corresponding argument must be a double*. You passed a char*. This
invokes undefined behavior. I think you meant %c.

if(scale==f || scale== F)
centTemp(userTemp)
centTemp returns a value. What do you do with that value?

printf("%f", userTemp);

system("PAUSE");
return 0;
}

double CentTemp(double a)

{

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

Remove del for email
Feb 24 '06 #14
"Antonio Contreras" <an*****@gmail.com> writes:
printf(" %lf \n", finalOutput);

The correct format specifier for printing doubles is %f, not %lf. In
fact if you read your manual pages you'll see that the l length
modifier cannot be applied to the f conversion specifier.

I'd still recommend using it, just to get into the habit. It's
incorrect to say that it cannot be applied: it's just that it has no effect.
Feb 24 '06 #15
Micah Cowan schrieb:
"Antonio Contreras" <an*****@gmail.com> writes:
printf(" %lf \n", finalOutput);

The correct format specifier for printing doubles is %f, not %lf. In
fact if you read your manual pages you'll see that the l length
modifier cannot be applied to the f conversion specifier.

I'd still recommend using it, just to get into the habit. It's
incorrect to say that it cannot be applied: it's just that it has no effect.

Depends on your implementation; in C89, l preceding anything
but d, i, o, u, x, X, or n invokes undefined behaviour, at
least by the last public draft.

Getting thousands of "not supported" warnings which could not
be turned of when "porting" a "portable" program to a platform
with another compiler family has certainly soured this "valuable
habit" for me.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 24 '06 #16
Micah Cowan wrote:

"Antonio Contreras" <an*****@gmail.com> writes:
printf(" %lf \n", finalOutput);

The correct format specifier for printing doubles is %f, not %lf. In
fact if you read your manual pages you'll see that the l length
modifier cannot be applied to the f conversion specifier.

I'd still recommend using it, just to get into the habit.

What's the advantage of getting into the habit of writing %lf,
when %f is the right format specifier for double?

--
pete
Feb 24 '06 #17
Micah Cowan <mi***@cowan.name> writes:
"Antonio Contreras" <an*****@gmail.com> writes:
> printf(" %lf \n", finalOutput);

The correct format specifier for printing doubles is %f, not %lf. In
fact if you read your manual pages you'll see that the l length
modifier cannot be applied to the f conversion specifier.

I'd still recommend using it, just to get into the habit. It's
incorrect to say that it cannot be applied: it's just that it has no
effect.

You're right, the 'l' in "%lf" has no effect -- but why do you
recommend using it? "%f" is the usual format for printing an argument
of type double.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Feb 24 '06 #18
On 2006-02-24, Micah Cowan <mi***@cowan.name> wrote:
"Antonio Contreras" <an*****@gmail.com> writes:
> printf(" %lf \n", finalOutput);

The correct format specifier for printing doubles is %f, not %lf. In
fact if you read your manual pages you'll see that the l length
modifier cannot be applied to the f conversion specifier.

I'd still recommend using it, just to get into the habit. It's
incorrect to say that it cannot be applied: it's just that it has no effect.

It produces undefined behavior. For example, it would be reasonable for
a pure c89 printf implementation to use the same internal flag for l and
L.
Feb 24 '06 #19
pete <pf*****@mindspring.com> writes:
Micah Cowan wrote:

"Antonio Contreras" <an*****@gmail.com> writes:
> printf(" %lf \n", finalOutput);

The correct format specifier for printing doubles is %f, not %lf. In
fact if you read your manual pages you'll see that the l length
modifier cannot be applied to the f conversion specifier.

I'd still recommend using it, just to get into the habit.

What's the advantage of getting into the habit of writing %lf,
when %f is the right format specifier for double?

Because %lf has the right meaning for scanf(), and the right intuitive
meaning in general.

But I wrote this before I realized that its inclusion before f was a
new feature to C99.
Feb 24 '06 #20
Jordan Abel <ra*******@gmail.com> writes:
On 2006-02-24, Micah Cowan <mi***@cowan.name> wrote:
"Antonio Contreras" <an*****@gmail.com> writes:
> printf(" %lf \n", finalOutput);

The correct format specifier for printing doubles is %f, not %lf. In
fact if you read your manual pages you'll see that the l length
modifier cannot be applied to the f conversion specifier.

I'd still recommend using it, just to get into the habit. It's
incorrect to say that it cannot be applied: it's just that it has no effect.

It produces undefined behavior. For example, it would be reasonable for
a pure c89 printf implementation to use the same internal flag for l and
L.

C90 specifically says that "%lf" invokes undefined behavior.

C99 says that the 'l' in "%lf" has no effect.

IMHO this is similar to the C99 rule that falling off the end of
main() is equivalent to executing "return 0;"; it causes some
previously undefined code to become defined, but is not helpful for
code that was correct in the first place.

There is no good reason to use "%lf" rather than "%f".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Feb 24 '06 #21
Keith Thompson wrote:
C99 says that the 'l' in "%lf" has no effect.

IMHO this is similar to the C99 rule that falling off the end of
main() is equivalent to executing "return 0;"; it causes some
previously undefined code to become defined, but is not helpful for
code that was correct in the first place.

Those were my two counter examples to Doug Gwyn's claim
that the standard doesn't cater to sloppy programmers.

--
pete
Feb 24 '06 #22

This is the problem that I have to solve :
<snip> 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

Using more symmetric equations might improve your coding:

C=((5/9)(F+40))-40
F=((9/5)(C+40))-40
Rod Pemberton
Feb 24 '06 #23
Thanks everyone who helped me out. I finally found my bugs and fixed
them.
I'm a very very novice programmer and some of the things that were
suggested to me I have not had any exposure to them so I could not
implement them.

Feb 26 '06 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 3 by: Kapil Khosla | last post by: Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to... 8 by: Gerald Aichholzer | last post by: Hello NG, I try to apply the following template