I'm not understanding something

R

RadiationX

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

Antonio Contreras

Several errors in your code. Read comments below.
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
 
R

RadiationX

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.
 
P

Pedro Graca

RadiationX said:
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
(very short) comments.

#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
specified in your intro to this article.



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

RadiationX

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?
 
R

RadiationX

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

Antonio Contreras

RadiationX said:
Here is my updated code, which will compile now but it wont let me
enter a scale.

Glad I could help you with my previous post. See some comments below.
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.

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
is not the computation that your assignment asked for. Rewrite it.

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.
 
R

RadiationX

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

Keith Thompson

Pedro Graca said:
RadiationX wrote: [...]
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.
 
R

RadiationX

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

John F

RadiationX said:
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
 
A

Antonio Contreras

RadiationX said:
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.
 
B

Barry Schwarz

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
 
M

Micah Cowan

Antonio Contreras said:
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.
 
M

Michael Mair

Micah said:
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
 
P

pete

Micah said:
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?
 
K

Keith Thompson

Micah Cowan said:
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.
 
J

Jordan Abel

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.
 
M

Micah Cowan

pete said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top