Thread: Celsius in Fahrenheit converter - does not run

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    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. #2
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    One problem is

    if(i=2)

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

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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.
    Last edited by Bayint Naung; 03-06-2011 at 02:33 PM. Reason: wrong order in printf.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    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. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ApeWithGrape View Post
    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 )
     {  
    
      // your code here
    
       return 0; 
    }

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    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. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ApeWithGrape View Post
    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. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    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. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rlevin View Post
    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
    Also Notepad++ is frequently recommended... Notepad++ | 5.8.7

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

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    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
    Last edited by ApeWithGrape; 03-06-2011 at 11:00 PM.

  11. #11
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by ApeWithGrape View Post
    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 .
    You can add
    system("pause"); to the end of your program (right before return 0; )

    All this does is run the "pause" command in your cmd.exe after it executes. However, this is not portable nor is system a standard C function, it will also give you a warning. But it should work as a quick 'n' dirty solution.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with almost solved problem
    By Kaname in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2009, 07:21 AM
  2. help! temp converter
    By cakestler in forum C Programming
    Replies: 9
    Last Post: 03-05-2009, 02:41 PM
  3. Conversion from Fahrenheit to Celsius problem
    By -Syntax in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2008, 08:56 PM
  4. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 03:01 AM