Celsius in Fahrenheit converter - does not run

This is a discussion on Celsius in Fahrenheit converter - does not run within the C Programming forums, part of the General Programming Boards category; Hello! I have a problem with my C program. I want to calculate Fahrenheit in Celsius and reverse, but it ...

1. Celsius in Fahrenheit converter - does not run

Hello!

I have a problem with my C program. I want to calculate Fahrenheit in Celsius and reverse, but it doesnīt work yet. Maybe I just got a mistake that I canīt figure out?

Additionally, is is there any possibility for shortening the if else statements? for example that the program doesnīt need printf in each if statement.

Thank you very much!

Code:
```//starts by including libaries
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

//Defines the two Function for calculating Celsius into Fahrenheit and reverse)

float CelsiusInFahrenheit(float Celsius)
{
return Celsius * 1.8 + 32;
}

float FahrenheitInCelsius(float Fahrenheit)
{
return (Fahrenheit - 32) / 1.8;
}

main()
{
int i;
float temperature;
printf("Type (1) for calculating Celsius in Fahrenheit or (2) for Fahrenheit in Celsius: \n");
scanf("%d",&i);

if(i=1) {
printf("Please type in the amount of Fahrenheit \n");
scanf("%f",&temperature);
printf("%4.2f Fahrenheit are %4.2f Celsius. \n", temperature, CelsiusInFahrenheit(temperature);
};
else if(i=2) {
printf("Please type in the amount of Celsius \n");
scanf("%f",&temperature);
printf("%4.2f Celsius are %4.2f Fahrenheit. \n", temperature, FahrenheitInCelsius(temperature);
}

else printf("Wrong input! Program will be closed");
}```
error messages:

D:\Programme\Dev-Cpp\CelsiusFahrenheitConverter in C.c: In function `main':

D:\Programme\Dev-Cpp\CelsiusFahrenheitConverter in C.c:33: error: syntax error before ';' token

D:\Programme\Dev-Cpp\CelsiusFahrenheitConverter in C.c:35: error: syntax error before "else"

D:\Programme\Dev-Cpp\CelsiusFahrenheitConverter in C.c:38: error: syntax error before ';' token

2. One problem is

if(i=2)

That is an assignment operator... you are looking for ==.

3. Code:
```struct Converter {
char *from;
char *to;
float (*convert)(float);
};

struct Converter Temp_converter[ ] = {
{ NULL,NULL}, // dummy
{ "Celsius"," Fahrenheit", CelsiusInFahrenheit },
{ "Fahrenheit","Celsius",FahrenheitInCelsius }
};

scanf("%d",&choice);  // choice = 1 or 2
if(choice != 1 || choice != 2)
die("wrong choice");

struct Converter c = Temp_converter[choice];
printf("Please type in the amount of %s \n",c.from);
scanf("%f",&temperature);
printf("%4.2f %s are %4.2f %s. \n", temperature,c.from, c.convert(temperature),c.to);```
Maybe an overkill.
Edit: maybe it's better to do this:
if( choice < 1 || choice > sizeof(Temp_converter)/sizeof(Temp_converter[0]) )
die("...");
then you can add other conversion if you wish later. (say celsius to kelvin)
You just need to write function and add it to table.

4. Hey!

Sorry, iīm very new into this topic, i canīt understand your program. Is it possible to rewrite my program with my statements? Because I donīt know statements like struct, die, sizeof etc. I want to learn it step by step

5. Originally Posted by ApeWithGrape
Hey!

Sorry, iīm very new into this topic, i canīt understand your program. Is it possible to rewrite my program with my statements? Because I donīt know statements like struct, die, sizeof etc. I want to learn it step by step
Yes, stay with your own code. Bayint's solution is like fishing with dynamite (and temperature conversion isn't a very big fish...).

1) You need to change your if() conditions ... if (a = b) assignes b to a then returns a, which always evaluates to true. However if(a == b) tests if the two are equal... which is what you want... so if(i == 1) and else if(i == 2) ... Note that spacing these things out a little makes it more visually obvious for troubleshooting...

2) the correct form of main in C is int main (void) and it returns a value at the end, usually 0...
Code:
```int main ( void )
{

return 0;
}```

6. Okay thank you very much for that!

But there are still some problems in these line which I canīt figure out:

printf("%4.2f Fahrenheit are %4.2f Celsius. \n", temperature, CelsiusInFahrenheit(temperature);

....

else if(i == 2) {

....

printf("%4.2f Celsius are %4.2f Fahrenheit. \n", temperature, FahrenheitInCelsius(temperature);

Can you or anyone tell me the right syntax, and maybe like i said an shorter, but easy program structure?

7. Originally Posted by ApeWithGrape
Can you or anyone tell me the right syntax, and maybe like i said an shorter, but easy program structure?
I gotta tell you... it don't get much shorter or easier than you've got.

About the only thing you could do is shorten your function names... CtoF and FtoC for example... but aside from that, it's pretty much at minimums now.

8. I'm a bit apprehensive as a new member to fix your code for you and I'm sure I'll be flamed if what I'm doing is not appropriate. However, I've tried to comment the code so you can see what was wrong. I wouldn't expect folks to normally do this.

You seriously need to pay attention to some basics:
1. don't put a semi colon between an if/else
2. understand the difference between assignment '=' and comparison '=='. In an if statement, you likely want comparison.
3. Properly nest parans, brackets, etc.,etc. In your case, you made function calls at the end of your printf's and forget to close the printf call itself.

For 3, you may want to use a text editor that will help clue you in with colors when you have badly nested code. As soon as I put your code in to Vim (my editor), it showed me many of these mistakes. I must confess that Vim is probably going to require too much time unless you are serious about programming, but many editors should help you this way.

Ok, here's the annotated code that I believe works as you intended:

Code:
```//starts by including libaries
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

//Defines the two Function for calculating Celsius into Fahrenheit and reverse)

float CelsiusInFahrenheit(float Celsius)
{
return Celsius * 1.8 + 32;
}

float FahrenheitInCelsius(float Fahrenheit)
{
return (Fahrenheit - 32) / 1.8;
}

/* main returns an int */
int main(void)
{
int i;
float temperature;
printf("Type (1) for calculating Celsius in Fahrenheit or (2) for Fahrenheit in Celsius: \n");
scanf("%d",&i);

if(i==1) {
//if(i=1) {  /* one '=' is assignment; you wanted '==' for comparison */
printf("Please type in the amount of Fahrenheit \n");
scanf("%f",&temperature);
printf("%4.2f Fahrenheit are %4.2f Celsius. \n", temperature, CelsiusInFahrenheit(temperature)); /* you were missing the closing paran here */

}/* don't put a semicolon between an if else */
else if(i==2) {
//else if(i=2) {
printf("Please type in the amount of Celsius \n");
scanf("%f",&temperature);
printf("%4.2f Celsius are %4.2f Fahrenheit. \n", temperature, FahrenheitInCelsius(temperature));  /* same: missing closing paran */
}
else { /* better to use brackets when you're learning so you don't get confused */
printf("Wrong input! Program will be closed\n");
}

return 0; /* main should return an int (zero for success) */
}```

9. Originally Posted by rlevin
For 3, you may want to use a text editor that will help clue you in with colors when you have badly nested code. As soon as I put your code in to Vim (my editor), it showed me many of these mistakes. I must confess that Vim is probably going to require too much time unless you are serious about programming, but many editors should help you this way.
For anything bigger than "Hello World" a syntax highlighting editor is worth it's weight in gold. Just the ability to put your cursor on a brace and see it's mate will help avoid half the erorrs you're likely to get.

One of the better ones is Code::Blocks... Code::Blocks

Most of the better ones also have build capabilities that can be most helpful.

10. Great, it works now Thank you for tell me that!

But i have one last problem: If I execute that program and I type in something it displays me for about half a second the result but then the windows closes immediately - what I can do against that? I have Dev-C++ and I want to run it in that compiler .

Edit: i noticed that it calculates wrong - when I type in 25 Celsius the result is -3.89 fahrenheit. Is there any wrong calculation? I canīt find it

11. Originally Posted by ApeWithGrape
Great, it works now Thank you for tell me that!

But i have one last problem: If I execute that program and I type in something it displays me for about half a second the result but then the windows closes immediately - what I can do against that? I have Dev-C++ and I want to run it in that compiler .