Sponsored By

blim

C Programming Help

Recommended Posts

Hi, this is son. I'm taking a class in C programming, I have to write a basic program converting fahrenheit to celsius and stuck on what to do, have a program written out, but it has mistakes, not quite sure what im doing... These are the program objectives that I have to follow:

"Write a program in C to convert a Fahrenheit temperature to a Celsiusm temperature and a program to convert a Celsius temperature to a Fahren heit temperature. The formulae for these conversions are fahrenheit = (celsius * 1.8f) + 32.0f and celsius = (fahrenheit - 32.0f) / 1.8f. Use floating point variables

Output will be sent to the screen and input will come from the keyboard. Use printf() and scanf() as required. To position output on the screen, use only \n (newline escape sequences). Other methonds of cursor positioning will not be accepted.

Do not make this assignment more complicated than it is! Do not include bells, whistles, dancing bears or the kitchen sink. Should you decide to do so, do not submit it to me. Please do not attempt to write these as one program even if you know how.

The ending screens should look like this when the temperatures listed are input

SCREENS:

Fahrenheit to Celsius Conversion

Enter Your Fahrenheit Temperature: 212

212.000000 Fahrenheit is 100.000003 Celsius

and

Celsius to Fahrenheit Conversion

Enter Your Celsius Temperature: 100

100.000000 Celsius is 211.999995 Fahrenheit"

I'm only working on the Fahrenheit to Celsius conversion right now. This is what I have for code right now, I know there's mistakes, I just don't know what (still very new and noobish in the ways of programming):

/* fahcel.c by Aron 09/27/2005 */

/*This program is used to convert Fahrenheit to Celsius. */

#include <stdio.h>

int main()

{

float float1, fahrenheit, celsius;

printf("Fahrenheit to Celsius Conversion\n\n");

printf("Enter Your Fahrenheit Temperature: ");

scanf("%f", &float1);

fahrenheit = (celsius * 1.8f) + 32.0f;

printf("\n\n%f Fahrenheit is %f Celsius", fahrenheit, celsius);

return 0;

}

Thank you for your help

-Aron (son)

Share this post


Link to post
Share on other sites

I'm not a C programmer, but I am a little able in PHP.

First, you are using an undefined variable here on the right side of the assignment operator ('=' sign). You assigned the input from the keyboard to float1, not celsius:

 fahrenheit = (celsius * 1.8f) + 32.0f;

Also that formula is wrong for the Fahrenheit to Celsius conversion.

You really only need two variables here, not three.

Share this post


Link to post
Share on other sites

Hmph... The code was given by the teacher... The error I get when running the program is:

F:\fahcel.c(15) : warning C4700: local variable 'celsius' used without having been initialized

Not sure if that helps.

Also, not quite sure what you mean by "First, you are using an undefined variable here on the right side of the assignment operator ('=' sign). You assigned the input from the keyboard to float1, not celsius:", should I change every instance of "float1" to "celsius"?

Share this post


Link to post
Share on other sites

Canoeingkidd is right, think about what you are reading, where it is going, what you are doing with it, and where you are putting the result. Just slapping together bits that you know doesn't work, you need to think about how they work together.

And a newline at the end of the last formatting string would be a nice touch. You don't need it but it'll make the output a bit nicer.

Also, not quite sure what you mean by "First, you are using an undefined variable here on the right side of the assignment operator ('=' sign). You assigned the input from the keyboard to float1, not celsius:", should I change every instance of "float1" to "celsius"?

Think about what you are doing, what value do you wan to read?

What do you want to do with it?

THEN think about how to to read that value.

THEN think about how to do what you need with that value to get the other one you want.

Don't just guess about changes to make and then "see if it works"

Edited by Hai-Etlik

Share this post


Link to post
Share on other sites

Ok... Umm, the input from the keyboard would be the "float float1, fahrenheit, celsius;" line? Should I delete the "float1" and move "celsius" forward? Sorry, I'm very much noobish when it comes to programming

Share this post


Link to post
Share on other sites

Don't think in terms of code, think of what the overall goal of the program is, then break it down into simpler steps. Then make sure your setps realy do accomplish the goal. Then think about how to convert the steps into code, if you can't think of a way, try breaking down a setp further.

Your problem is you are jumping ahead and trying to fill in with guesses. You will eventualy be able to streamline the process as you get better, but fow now you realy need to go slowly and steadily.

Right now you are making a mistake that you will probably only notice if you go right back to the very top level.

Share this post


Link to post
Share on other sites

I kinda got the program to work. I got to the point to where there's no errors and I can actually run the program, but now the input is being put into celsius spot and solving for fahrenheit, and I need it to go into the fahrenheit spot not the celsius spot. Here's a post of my newest attempt:

/* fahcel.c by Aron 09/27/2005 */

/* This program is used to convert Fahrenheit to Celsius. */

#include <stdio.h>

int main()
{
float celsius, fahrenheit;
 
printf("Fahrenheit to Celsius Conversion\n\n");
printf("Enter Your Fahrenheit Temperature: ");
scanf("%f", &celsius);

fahrenheit = (celsius * 1.8f) + 32.0f;

printf("\n\n%f Fahrenheit is %f Celsius\n", fahrenheit, celsius);

return 0;
}

Added a newline character at the end of the final printf line, changed the scanf line and the float line

Share this post


Link to post
Share on other sites

Let's try this the other way arround

 scanf("%f", &celsius);

fahrenheit = (celsius * 1.8f) + 32.0f;

Think about what these to lines do, and why you are doing that.

These are the lines that realy do stuff the rest is window dressing and boilerplate.

Share this post


Link to post
Share on other sites

I solved a problem before reading your post, switched a few words around and was able to make the celsius to fahrenheit conversion, so got the problem that I wasn't working on done, thankfully. Now I'll try fahrenheit again with the help from your last post.

Here's the results from my successful program, the celsius to fahrenheit program:

/* fahcel.c by Aron 09/27/2005 */

/* This program is used to convert Celsius to Fahrenheit. */

#include <stdio.h>

int main()
{
float celsius, fahrenheit;
 
printf("Celsius to Fahrenheit Conversion\n\n");
printf("Enter Your Celsius Temperature: ");
scanf("%f", &celsius);

fahrenheit = (celsius * 1.8f) + 32.0f;

printf("\n\n%f Celsius is %f Fahrenheit\n\n", celsius, fahrenheit);

return 0;
}

*edit*

WOOHOO!!! Found my problem, fixed it, and now I'm done! Fixed the lines that you suggested, added fahrenheit in the scanf line instead of celsius, and put in the other formula.

Here's my final code for the fahrenheit:

/* fahcel.c by Aron Briggs 09/27/2005 */

/* This program is used to convert Fahrenheit to Celsius. */

#include <stdio.h>

int main()
{
float celsius, fahrenheit;

printf("Fahrenheit to Celsius Conversion\n\n");
printf("Enter Your Fahrenheit Temperature: ");
scanf("%f", &fahrenheit);

celsius = (fahrenheit - 32.0f) / 1.8f;

printf("\n\n%f Fahrenheit is %f Celsius\n\n", fahrenheit, celsius);

return 0;
}

I'd like to thank Canoeingkidd and a big thank you to Hai-Etlik for your guys' help

Edited by blim

Share this post


Link to post
Share on other sites

Hi!!! Liz, here. I also want to thank you two for encouraging Aron and helping him solve his C++ problem. I especially appreaciate you not giving him the answers outright (I knew you did this when he wailed, "bahhhhh"!) and making him think :thumbsup: He needs to keep his grades up to keep his scholarships, but this stuff has me totally helpless (I just see gobbly gook) so it sure is nice to say, "ask the forumfolks". You are our Safety Net.

Thanks again

Love,

Liz

Edited by blim

Share this post


Link to post
Share on other sites

He's still pretty much undecided for a major---he's good at computing stuff (well, so far) and sort of leaning towards teaching because so many folks have told him he would be good at it (he's kind of a big eccentric kid....and they do make the best teachers, don't they? ;) )

He does need to decide pretty quickly, though, because in December he will have his Associates/Transfer degree, so I guess he is almost a Junior. Don't want him to go through the "major-changing routine" and spend more time and money in school because of it!!!

Liz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now