I going back to school and need help

I wanted to go back to school to learn more the bad thing it is an online learning class. I have emailed the Instructor a few times and have not got any response. This is what I have done all alone. My biggest issues it that my final outcome is 8 or more numbers. I have gone through the book and it is not helping. I have to make a temperature converter with user options. for either C or F. Can anyone look and point me on the right path please.

#include <stdlib.h>
#include <conio.h>
#include <math.h>

int main()
{
int opt;
int input;
float c;
float f;


system("COLOR 1C");
printf("Welcome to the Temperature Converter Program\n\n");

printf("Please choose one of the following:\n");
printf("Press (1) for Farenheit to Celsius.\n");
printf("Press (2) for Celsius to Farenheit.\n");
scanf("%d", &opt);

printf("Please enter Temperature:\n");
scanf("%d", &input);

if (opt==1)
{
c=(input-32.)*5.0/9.0;
printf("Temperature is in Celsius %d\n", &c);
}
else if (opt==2)
{
f=(input+32.0)*9.0/5.0;
printf("Temperature is in Farenheit %d\n", &f);
}


_getch();
return 0;
}
The issue you are describing is because of this line:

printf("Temperature is in Celsius %d\n", &c);

printf() is expecting a decimal and you are giving it a float.

Change the %d to %f so that printf() will use the float. That will give you much better results.

Good luck!
Last edited on
When I do that an I press (1) for Farenheit to Celsius and enter a degree of 100 my Celsius temp is -1.#QNAN0. So is my function correct or did I do something else wrong.
Last edited on
First, the use of code tags is strongly recommended for code samples. See http://www.cplusplus.com/articles/jEywvCM9/

Second, I don't think that your equations are correct. Lets start from F, convert to C and back to F:
1
2
3
4
5
6
7
8
9
C = (F - 32.0) * 5.0 / 9.0;

F = (C + 32.0) * 9.0 / 5.0;
// substitute C
F = ( (F - 32.0) * 5.0 / 9.0 + 32.0) * 9.0 / 5.0;
F = ((F - 32.0) * 5.0 / 9.0 * 9.0 / 5.0) + (32.0 * 9.0 / 5.0);
F = (F - 32.0) + (32.0 * 9.0 / 5.0);
F - F = -32.0 + (32.0 * 9.0 / 5.0)
0 == 32 * 8 / 5 // oh my 


outcome is 8 or more numbers

Do you mean that there are many decimals in the printed values?

The format parameter of printf() can solve that. Precision. See http://www.cplusplus.com/reference/cstdio/printf/
By looking at yours i think I enter my equations correct. The issue I have if is that my answer is only has one decimal and a bunch of letters. I have worked on this for 3 days and this is how far I have gotten. I have did nothing but read my book and watch youtube videos.
You've got two issues:

1. printf() behaves differently than you expect.

Check the documentation at: http://www.cplusplus.com/reference/cstdio/printf/

One misplaced character will give you unexpected results.

2. Your equations are not correct. Close, but a little off. Double check them. Add parenthesis, sometimes we assume a certain order of operations and we are wrong.


I'd fix the printf() problem first, then you can more easily check your equations. Just set "c" to a fixed value and see what it prints out. Once that's working then deal with the equations.
Last edited on
I would like to thank everyone who has shared there knowledge with me. But it appears I am getting more confused than ever. I did change my printf to ( "%f"). Also I will relook at my equation. But one thing should I remove my int input?
I apologize for being cryptic. I have a hard time balancing "just giving the answer" and "teaching".

You are using the address-of operator(&) when you use printf(). So you are sending the address of c, that will give you some unknown value.

Just send the variable. Like so:

printf("Temperature is in Celsius %f\n", c);

Insead of:

printf("Temperature is in Celsius %d\n", &c);



Last edited on
Are you !%$@$ me it was the stupid (&) in my printf that was missing me up. OMG I need to kick myself really hard in "you know what" I have been reading the links that you and others have provided me I am rereading my book. I feel really stupid that something so simple drove me to the point I want to quit. I thank you mpacker and I would like to say sorry. I would like it more if you teach me.

P.S
You are right my equation is wrong I will look into it.
Last edited on
Haha, we've all been there, the good thing is you're a lot less likely to make that mistake again.
Are you !%$@$ me it was the stupid (&) in my printf that was missing me up. OMG I need to kick myself really hard in "you know what"

This is why we encourage beginners to use the iostream instead of the C library. It is type safe.

As for the equations. It looks like your conversion from F to C is correct, but C to F is not. To fix it, just take the correct one:
C = (F - 32.0) * 5.0 / 9.0;
and solve for F.

f=(input * 9.0) / 5.0 + 32;
printf("Temperature is in Farenheit %.f\n", f);

This is the way I went and it looks like it is working like a champ. Thank you
Topic archived. No new replies allowed.