-1

I am trying to combine Switch into Void Function to convert Celsius to Fahrenheit and vice versa. I am not sure what is correct way to combine Switch and Void Function together. My code is at below. Any advice is valuable for me. Thank you.

#include<stdio.h>
void fahtocel(void);
void celtofah(void);
int main(void)
 {
    int tem;
    printf("Please select your choice\n");
    printf("Enter 1 if you need to convert Fahrenheit to Celsius\n");
    printf("Enter 2 if you need to convert Celsius to Fahrenheit\n");
    scanf("%d", &tem);
    switch(tem) {
case 1:
    fahtocel();
    break;
case 2:
    celtofah();
    break;
}
return 0;
}

void fahtocel()
{
 float fahr;
   printf("Please input Fahrenheit: ");
   scanf("%2.f", &fahr);
   printf("%3f Fahrenheit is equal %3.0f Celsius \n",fahr,(5.0/9.0)*(fahr - 32)*5/9;
} 
void celtofah()
{
 float celsius;
 printf("Please input celsius: ");
 scanf("%2.f", &celsius);
 printf("%2.f Celsius is equal %2.f Fahrenheit\n",celsius, (9*celsius/5)+32);
 }
  • What's the problem? What doesn't this code do correctly? – Carcigenicate Jan 02 '18 at 23:49
  • 1
    `switch` is not an input statement. Try adding `scanf("%d", &tem);` after the `printf()` lines. And please try to indent your code properly before posting. – r3mainer Jan 02 '18 at 23:49
  • I am using Code Block, my code is not running, i believe because the way i am using switch with Void function is not correct – James Macathy Jan 02 '18 at 23:51
  • 3
    Your maths is all wrong too. What is `(9.0*celsius)/(5.0+32)` supposed to do? – r3mainer Jan 02 '18 at 23:51
  • i changed the math to correct one printf("%2.f Celsius is equal %2.f Fahrenheit\n",celsius, (9*celsius/5)+32); – James Macathy Jan 02 '18 at 23:55
  • `tem` in not initialized. Your code won't do what you want unless you can put something into that value. See the `scanf()` comment above. – Michael Dorgan Jan 02 '18 at 23:57
  • Hi all Helpers, i changed the math and add scanf after printf, but after running, the code still does not show me correct conversion – James Macathy Jan 03 '18 at 00:05
  • @MichaelDorgan `tem` is initialized – Burstful Jan 03 '18 at 00:05
  • I like: `F = (9.0/5.0) * (C + 40.0) - 40.0` and `C = (5.0/9.0) * (F + 40.0) - 40.0`. It's nicely symmetric, relying on -40C = -40F. – Jonathan Leffler Jan 03 '18 at 00:09
  • Try `%2.2f` while in your printf.. and also do the math pefore your printf instead of in it.. like this `float temp = 0.0; temp = (5.0/9.0)*(fahr - 32)*5/9; //Not saying this math is correct or anything printf("%3f Fahrenheit is equal %3.0f Celsius \n",fahr,temp);` – Burstful Jan 03 '18 at 00:09
  • @JamesMacathy you must be getting compiler errors with this code. Care to share? – Burstful Jan 03 '18 at 00:10
  • @CoreyLakey i did as you advice, but it still show the wrong output. – James Macathy Jan 03 '18 at 00:17
  • @CoreyLakey my code block is working well with other program i code. so i think the cause is from my own code. Is this correct way to use Swtich with Void Funtions? So far, everything running but the Output is wrong – James Macathy Jan 03 '18 at 00:19
  • @JamesMacathy Compiler errors does not mean your IDE is broken. Compiler errors means the compiler is telling you that your code in your current project couldn't be compiled. Read the compiler errors. Do you have them enabled in "Logs&Others"? – Burstful Jan 03 '18 at 00:27

1 Answers1

0

You must be getting compiler issues. Put a parentheses at the end of your printf, and change the %2.f to just %f in your scanfs.

This code works...

#include<stdio.h>
void fahtocel(void);
void celtofah(void);
int main(void)
{
    int tem;
    printf("Please select your choice\n");
    printf("Enter 1 if you need to convert Fahrenheit to Celsius\n");
    printf("Enter 2 if you need to convert Celsius to Fahrenheit\n");
    scanf("%d", &tem);
    switch (tem) {
    case 1:
        fahtocel();
        break;
    case 2:
        celtofah();
        break;
    }
    return 0;
}

void fahtocel()
{
    float fahr;
    printf("Please input Fahrenheit: ");
    scanf("%f", &fahr);
    printf("%3f Fahrenheit is equal %3.0f Celsius \n", fahr, (5.0 / 9.0) * (fahr - 32) * 5 / 9);
}
void celtofah()
{
    float celsius;
    printf("Please input celsius: ");
    scanf("%f", &celsius);
    printf("%2.f Celsius is equal %2.f Fahrenheit\n", celsius, (9 * celsius / 5) + 32);
}
Burstful
  • 347
  • 1
  • 10
  • By the way, if i want after the 1st time conversion, the menu appears again to allow user choose to convert and input again. I guess i need to use while or for loop, but not sure how to use it. – James Macathy Jan 03 '18 at 00:34
  • @JamesMacathy If you want it to go again, start by wrapping main in a while loop from your first printf to the end of your switch statement. – Burstful Jan 03 '18 at 00:37
  • @JamesMacathy The fact that your issues were minor compiler errors, tells me you either have no compiler errors enabled or don't know where to find them in your log.. and must have a look at https://stackoverflow.com/questions/4456165/enable-compiler-output-pane-in-codeblocks – Burstful Jan 03 '18 at 00:42
  • my compiler was still running and show the math, but it just wrong. after i only changed %2.f to %f as you said, it works. Thanks a lot. I look into while loop now. – James Macathy Jan 03 '18 at 00:46
  • Ok great. @JamesMacathy – Burstful Jan 03 '18 at 00:47