Thread: Help with functions & switch case in my converting program

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    33

    Help with functions & switch case in my converting program

    So my first time using functions and still dont think I've grasped them.
    Program is to convert Fahrenheit to Celsius and vice verses. Using a switch for the menu to find out what you want to do, but each time you enter either option the program quits.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void menu(){
        printf("1. To convert from Fahrenheit to Celsius enter 1\n");
        printf("2. To convert from Celsius to Fahrenheit enter 2\n");
        printf("3. Quit\n");
        printf("What would you like to do?\n");
        printf("\n");
    }
    
    void celsiusToFahrenheit(){
        int F=0, C=0;
    
        printf("Enter a value you'd like to convert\n");
        scanf("%d", &C);
        F = 32 + C*1.8;
        return C;
    }
    
    void fahrenheitToCelsius(){
        int F=0, C=0;
    
        printf("Enter a value you'd like to convert\n");
        scanf("%d", &F);
        C = (5/9)*(F-32);
        return C;
    }
    
    int main(){
        int choice=0;
    
        menu();
        scanf("%d", &choice);
        switch(choice){
        case '1': fahrenheitToCelsius();
            break;
        case '2': celsiusToFahrenheit();
            break;
        case '3': return 0;
        }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    If you can tell the different between '1' and 1, you know what's wrong here.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    int choice=0;
    
    scanf("%d", &choice);
    
    case '1':
    case '2':
    case '3':
    You're reading an integer, but '1', '2', and '3' (in single quotes) represent the character glyphs for these numbers. These are not equivalent to the integers 1, 2, and 3. So get rid of the single quotes for those cases.

    It would be a good idea to include the "default" case in your "switch()", perhaps with an "invalid input" message, so you can get more information on what is going on in your program. It would have helped show you that none of your inputs was valid.



    If a function is supposed to return a value, then it needs to be declared as returning the expected type. If a function is not to return a value, the return type should be "void".

    Your two functions have a return type of "void", but you're trying to return values from them.

    You have two options:

    1. Keep their return type "void", and just print the result directly in the function;
    2. Change their return type to the data type you want to return, return it from the function, and print it in "main()". Note that you need a variable of the same type in "main()" to store the return value.

    (There is a shorthand method of printing the return value of a function without creating a separate variable for it, but I won't describe that just yet.)

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Wow thanks. Totally missed that :P I got it working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  2. Functions in a switch case...
    By yaya in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2008, 03:58 AM
  3. switch /case
    By shuo in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2007, 06:43 PM
  4. switch/case & functions
    By Torsin in forum C++ Programming
    Replies: 8
    Last Post: 09-11-2003, 10:50 AM
  5. Case and switch
    By CAP in forum C Programming
    Replies: 5
    Last Post: 07-13-2002, 03:16 PM