Thread: Temperature converter program with functions

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    37

    Question Temperature converter program with functions

    Hello again.

    I need help with this program. I need to connect the functions but I am having trouble doing so. The requirements are in the code. This is what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FIRSTCHOICE 1
    #define SECONDCHOICE 2
    #define THIRDCHOICE 3
    
    /* Write the code using functions. Write a program that gives user menu to choose 
    from either convert temperature input from the user in degrees Fahrenheit to degrees
    Celsius or convert temperature input from the user in degrees Celsius to degrees
    Fahrenheit. Then, another option to quit the program. */
    
    void menu_message(int input) {
     
     printf("%d. Fahrenheit to Celsius \n", FIRSTCHOICE);
     printf("%d. Celsius to Fahrenheit \n", SECONDCHOICE);
     printf("%d. Quit the program \n", THIRDCHOICE);
    
    }
    
    double celsius_to_fahrenheit(int input) { // Choice 1
     
     int result;
     
     result = (9/5) * input + 32;
     
     return result;
    
    }
    
    double fahrenheit_to_celsius(int input) { // Choice 2
     
     int result;
     
     result = (5/9) * (input - 32);
     
     return result;
    
    }
    
    void quit_message(){ // Choice 3
     printf("Goodbye. \n");
    
    }
    
    main() {
     
     int fahrenheit, celsius, quit, input;
     
     menu_message(input); // Menu to choose from
     scanf("%d", &input); // Get a value from the user
     fahrenheit = celsius_to_fahrenheit(input); // Choice 1
     celsius = fahrenheit_to_celsius(input); // Choice 2
     quit = quit_message(); // Choice 3
     
     return 0;
    
    }
    Am I using scanf in the right place. If not, which function does it need to be placed in? And am I using the functions correctly?

    Thank you.
    Last edited by Kayla Hoyte; 12-03-2017 at 03:39 PM. Reason: neatness

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Right now you're passing the user's choice as a temperature to both functions. I'd take a wild guess and say this isn't what you want... Use a switch statement for the different choices and ask again for the number to pass to them.

    By the way, quit_message() returns nothing, you can't assign a void function into anything. You don't need an assignment for that function is what I'm trying to say.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look up integer division in C.

    5/9 equals 0 in C

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by GReaper View Post
    Right now you're passing the user's choice as a temperature to both functions. I'd take a wild guess and say this isn't what you want... Use a switch statement for the different choices and ask again for the number to pass to them.

    By the way, quit_message() returns nothing, you can't assign a void function into anything. You don't need an assignment for that function is what I'm trying to say.

    Yeah I want the uset input to be put as the input in either equation. Where would I put the switch statement?

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by stahta01 View Post
    Look up integer division in C.

    5/9 equals 0 in C

    Tim S.
    So as a result I have to use doubles?

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    are you allowed to use switches yet, what about while loops?
    write two functions, one for Celsius, one for Fahrenheit,
    Code:
    double MyFunction( double input)
    {
      return input / 34;
    }
    that is just an example of a shorter from of doing math in a function and returning it at the same time.

    You know what condition loops have to have in order to enter them, you know how to change a variable to make it what you need to have it in order for it to leave a while loop, You know how to use a switch and a char as an int. you know how to use printf and return values from functions together, simultaneously in order to print out the results of the return value, you know how to use printf in general. piece of cake,

    one while loop
    one switch,
    two functions
    two vars: 1 char,1 double
    and a few printf's for good measure
    mixed logically
    done.
    covers the everything
    /* Write the code using functions. Write a program that gives user menu to choose
    from either convert temperature input from the user in degrees Fahrenheit to degrees
    Celsius or convert temperature input from the user in degrees Celsius to degrees
    Fahrenheit. Then, another option to quit the program. */
    needing an option to quit most always suggest a loop, if not always (otherwise the program would just process then quit on its own). Some teachers like to be "tricky" sometimes to see who is awake and who is not.
    and you need to check your math on the conversions
    Temperature Conversion Celsius to Fahrenheit | F to C or C to F | The Old Farmer's Almanac
    so you'll end up with something that might look like this
    Code:
    $ ./Kayla_Hoyte_Temperature_converter
    Enter a selection to convert temperatures.
    1. Celsius to Fahrenheit
    2. Fahrenheit to Celsius.
    3. Quit
    1
    enter temperature in Celsius
    -30
    
    Celsius -30.00
    Fahrenheit -22.00
    
    Enter a selection to convert temperatures.
    1. Celsius to Fahrenheit
    2. Fahrenheit to Celsius.
    3. Quit
    2
    enter temperature in Fahrenheit
    40
    
    Fahrenheit 40.00
    Celsius 4.44
    
    Enter a selection to convert temperatures.
    1. Celsius to Fahrenheit
    2. Fahrenheit to Celsius.
    3. Quit
    3
    
    addio
    Last edited by userxbw; 12-04-2017 at 07:53 AM.

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by userxbw View Post
    are you allowed to use switches yet, what about while loops?
    write two functions, one for Celsius, one for Fahrenheit,
    Code:
    double MyFunction( double input)
    {
      return input / 34;
    }
    that is just an example of a shorter from of doing math in a function and returning it at the same time.

    You know what condition loops have to have in order to enter them, you know how to change a variable to make it what you need to have it in order for it to leave a while loop, You know how to use a switch and a char as an int. you know how to use printf and return values from functions together, simultaneously in order to print out the results of the return value, you know how to use printf in general. piece of cake,

    one while loop
    one switch,
    two functions
    two vars: 1 char,1 double
    and a few printf's for good measure
    mixed logically
    done.
    covers the everything

    needing an option to quit most always suggest a loop, if not always (otherwise the program would just process then quit on its own). Some teachers like to be "tricky" sometimes to see who is awake and who is not.
    and you need to check your math on the conversions
    Temperature Conversion Celsius to Fahrenheit | F to C or C to F | The Old Farmer's Almanac
    so you'll end up with something that might look like this
    Code:
    $ ./Kayla_Hoyte_Temperature_converter
    Enter a selection to convert temperatures.
    1. Celsius to Fahrenheit
    2. Fahrenheit to Celsius.
    3. Quit
    1
    enter temperature in Celsius
    -30
    
    Celsius -30.00
    Fahrenheit -22.00
    
    Enter a selection to convert temperatures.
    1. Celsius to Fahrenheit
    2. Fahrenheit to Celsius.
    3. Quit
    2
    enter temperature in Fahrenheit
    40
    
    Fahrenheit 40.00
    Celsius 4.44
    
    Enter a selection to convert temperatures.
    1. Celsius to Fahrenheit
    2. Fahrenheit to Celsius.
    3. Quit
    3
    
    addio
    aahahaha I'm really bad at programming, my apologies.

    Yes i can use anything else but I have to use at least two functions. Also yes, that's what I'm looking for. How exactly would I add the switch and loops?

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    aahahaha I'm really bad at programming, my apologies.

    Yes i can use anything else but I have to use at least two functions. Also yes, that's what I'm looking for. How exactly would I add the switch and loops?
    aahahaha you should have practice your loops more.
    How would you exactly add a switch and a loop. Let me think.......
    you do know that the editors in Computers do have the ability to modify the text you write inside of the file yes?
    SO that requires a keyboard. using your keyboard type in the the loop how it is written, same thing for a switch. That's pretty simple, if you do not know how to type ,which you have to else you'd not have been able to post this question.

    now apply the rules of trial and error until you get it right, because you should have at least covered loops and a little bit on switches before getting to how to write functions. in order to know how to put them together to write this program.

    where would you put the loop and where would you place the switch ? Please show us in here your logic.

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    37

    Arrow

    Quote Originally Posted by userxbw View Post
    aahahaha you should have practice your loops more.
    How would you exactly add a switch and a loop. Let me think.......
    you do know that the editors in Computers do have the ability to modify the text you write inside of the file yes?
    SO that requires a keyboard. using your keyboard type in the the loop how it is written, same thing for a switch. That's pretty simple, if you do not know how to type ,which you have to else you'd not have been able to post this question.

    now apply the rules of trial and error until you get it right, because you should have at least covered loops and a little bit on switches before getting to how to write functions. in order to know how to put them together to write this program.

    where would you put the loop and where would you place the switch ? Please show us in here your logic.
    Yeah, I deserved that.

    Moving on, I think I figured it out except the integer division in which I use decimals.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FIRSTCHOICE 1
    #define SECONDCHOICE 2
    #define THIRDCHOICE 3
    /* Write the code using functions. Write a program that gives user menu to choose 
    from either convert temperature input from the user in degrees Fahrenheit to degrees
    Celsius or convert temperature input from the user in degrees Celsius to degrees
    Fahrenheit. Then, another option to quit the program. */
    menu_message(int input) {
     
     printf("Choose from the options below(1-3): \n");
     printf("%d. Celsius to Fahrenheit \n", FIRSTCHOICE);
     printf("%d. Fahrenheit to Celsius \n", SECONDCHOICE);
     printf("%d. Quit the program \n", THIRDCHOICE);
    }
    double celsius_to_fahrenheit(int input) { // Choice 1
     
     double result;
     
     result = (1.8) * input + 32;    
     
     return result;
    }
    double fahrenheit_to_celsius(int input) { // Choice 2
     
     double result;
     
     result = (.56) * (input - 32);
     
     return result;
    }
    main() {
     
     int choice, input;
     
     printf("Enter the temperature in Fahrenheit (F) or Celsius (C): \n");
     scanf("%d", &input);
     
     menu_message(input); // Menu to choose from
     scanf("%d", &choice); // Get a value from the user
     switch(choice) {
      case 1:
       printf("The temperature from Celsius to Fahrenheit is: %.1lf", celsius_to_fahrenheit(input));
       break;
      case 2:
       printf("The temperature from Fahrenheit to Celsius is: %.1lf", fahrenheit_to_celsius(input));
       break;
      default:
       printf("Thank you. Goodbye.");
     }
     return 0;
    }

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    Yeah, I deserved that.

    Moving on, I think I figured it out except the integer division in which I use decimals.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FIRSTCHOICE 1
    #define SECONDCHOICE 2
    #define THIRDCHOICE 3
    /* Write the code using functions. Write a program that gives user menu to choose 
    from either convert temperature input from the user in degrees Fahrenheit to degrees
    Celsius or convert temperature input from the user in degrees Celsius to degrees
    Fahrenheit. Then, another option to quit the program. */
    menu_message(int input) {
     
     printf("Choose from the options below(1-3): \n");
     printf("%d. Celsius to Fahrenheit \n", FIRSTCHOICE);
     printf("%d. Fahrenheit to Celsius \n", SECONDCHOICE);
     printf("%d. Quit the program \n", THIRDCHOICE);
    }
    double celsius_to_fahrenheit(int input) { // Choice 1
     
     double result;
     
     result = (1.8) * input + 32;    
     
     return result;
    }
    double fahrenheit_to_celsius(int input) { // Choice 2
     
     double result;
     
     result = (.56) * (input - 32);
     
     return result;
    }
    main() {
     
     int choice, input;
     
    1.  printf("Enter the temperature in Fahrenheit (F) or Celsius (C): \n");
    
     2. scanf("%d", &input);
     
    3.  menu_message(input); // Menu to choose from
    //returns for that function then
      4. scanf("%d", &choice); // Get a value from the user
    // your choice var is then used here 
    5. switch(choice) {
    // so no need to pass in input to menu_message
    //because it is used where?
      case 1:
       printf("The temperature from Celsius to Fahrenheit is: %.1lf", celsius_to_fahrenheit(input));
       break;
      case 2:
       printf("The temperature from Fahrenheit to Celsius is: %.1lf", fahrenheit_to_celsius(input));
       break;
      default:
       printf("Thank you. Goodbye.");
     }
     return 0;
    }
    some fixing is needed.
    Code:
    gcc -Wall -Wextra -Wpedantic -lm -o "term2" "term2.c" (in directory: /home/userx/bin)
    term2.c:10:1: warning: return type defaults to 'int' [-Wimplicit-int]
     menu_message(int input) {
     ^
    term2.c: In function 'menu_message':
    term2.c:10:18: warning: unused parameter 'input' [-Wunused-parameter]
     menu_message(int input) {
                      ^
    term2.c: At top level:
    term2.c:33:1: warning: return type defaults to 'int' [-Wimplicit-int]
     main() {
     ^
    term2.c: In function 'menu_message':
    term2.c:16:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    Compilation finished successfully.
    1. no use of loop, just decided to skip that important part I see.
    2. menu function what is missing, and what is there that does not need to be there?
    3. main function what is missing?
    4. I'd put a '\n' end line on your print out's for good measure.
    5. if you do not want anything after the decimal point just make that one 1 a zero 0

    not too shabby, I'd like to see that while loop in use though. Hope your teacher doesn't.
    just need to keep in mind the order of execution. .. going to put it in number in your code in here.
    Last edited by userxbw; 12-04-2017 at 07:20 PM.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, at the moment you are doing it the way I prefer anyway - that is to say that the fractional numbers are written out as 1.8 and such. But to trigger decimal division all you have to do is divide decimal numbers: 9.0 / 5.0

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    now that you got your functions correct, this is how I do mine, just to show you coding styles.
    Code:
    double celsius_to_fahrenheit(double input) { // Choice 1
        return input * 1.8 + 32;
    }
     
    double fahrenheit_to_celsius(double input) { // Choice 2
        return  (input - 32) * .5556;
    }

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Choice three: (f-32)/1.8

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    just a hint to see if you can figure out how to take what is useful in this code to add it to your other code to make it more teacher friendly and program digestible. (for a lack of a better way of putting it)

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main (void)
    {
        printf("Would you like to see a dog chasing his tail? y/n\n");
        
        char what = '\0';
        int whatnow = 0;
        
        scanf(" %c", &what);
        
        while (tolower(what) != 'n')
        {
            printf("I'm chasing my tail\n");
            whatnow++;
            
            if (whatnow == 10)
            {
                printf("Do you still want to see me\nchasing my tail? y/n\n");
            
                if ( (scanf(" %c", &what)) == 'n')
                    what = 'q';
                else
                    whatnow = 0;
            }
        }
                
        
        
    return 0;    
    }

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    just a hint to see if you can figure out how to take what is useful in this code to add it to your other code to make it more teacher friendly and program digestible. (for a lack of a better way of putting it)
    So, you purposefully wrote an if statement that is never true?
    Code:
    (gdb) break 22
    Breakpoint 1 at 0x40160b: file userxbw.c, line 22.
    (gdb) run
    Starting program: C:\Users\jk\Desktop\a.exe
    [New Thread 1348.0x5a4]
    [New Thread 1348.0x1330]
    Would you like to see a dog chasing his tail? y/n
    y
    I'm chasing my tail
    I'm chasing my tail
    I'm chasing my tail
    I'm chasing my tail
    I'm chasing my tail
    I'm chasing my tail
    I'm chasing my tail
    I'm chasing my tail
    I'm chasing my tail
    I'm chasing my tail
    Do you still want to see me
    chasing my tail? y/n
    
    Breakpoint 1, main () at userxbw.c:22
    22                  if ( (scanf(" %c", &what)) == 'n')
    (gdb) next
    n
    25                      whatnow = 0;
    (gdb) c
    Continuing.
    [Thread 1348.0x1330 exited with code 0]
    [Inferior 1 (process 1348) exited normally]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temperature analyzer C program help
    By BlackUmbreon in forum C Programming
    Replies: 12
    Last Post: 02-17-2017, 04:17 PM
  2. Temperature conversion program bug.
    By prafiate in forum C Programming
    Replies: 6
    Last Post: 08-13-2012, 09:40 AM
  3. Temperature Conversion Program
    By llind212 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2011, 11:37 PM
  4. Need help with temperature conversion program
    By Aequitas in forum C Programming
    Replies: 4
    Last Post: 03-06-2011, 08:28 PM
  5. military time converter using functions
    By rfoor in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2010, 09:52 PM

Tags for this Thread