Thread: problem with functions

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    problem with functions

    Hey guys! im having some problems with functions. Just started going over them today in class and i was doing great throughout the semester until now! totally lost and confused! we were asked to make a program that contains 10 different functions for conversion and i cant even figure the first one out! This is what i have so far but i cant seem to get it to compile correctly...

    Code:
    #include<stdio.h>
    
    float milesToKilo(float input, float output)
    {
        output=0;    
        output=input*1.609344;
        printf("Thats %f kilometers!", output);
        return output;
    }
    
    float kiloToMiles();
    
    float inchesToCent();
    
    float centToInches();
    
    float milesToFeet();
    
    float feetToMiles();
    
    float cupsToQuarts();
    
    float quartsToCups();
    
    float litersToQuarts();
    
    float quartsToLiters();
    
    int main (void)
    {
    
        int choice=0;
        float input=0;
        float output=0;
    
        printf("Convert Miles to Kilometers................1\n");
        printf("Convert Kilometers to Miles................2\n");
        printf("Convert Inches to Centameters..............3\n");
        printf("Convert Centameters to Inches..............4\n");
        printf("Convert Miles to Feet......................5\n");
        printf("Convert Feet to Miles......................6\n");
        printf("Convert Cups to Quarts.....................7\n");
        printf("Convert Quarts to Cups.....................8\n");
        printf("Convert Liters to Quarts...................9\n");
        printf("Convert Quarts to Liters..................10\n");
        printf("What would you like to do?\n");
        scanf("%d", &choice);
    
        switch (choice)
            {    
            case 1:
                printf("How many miles?\n");
                scanf("%f", &input);
                output=milesToKilo();            
                printf("That would be %f Kilometers!\n", milesToKilo);
                break;
            case 2:
                printf("Kilometers to Miles!\n");
                break;
            case 3:
                printf("Inches to Centameters!\n");
                break;
            case 4:
                printf("Centameters to Inches!\n");
                break;
            case 5:
                printf("Miles to Feet!\n");
                break;
            case 6:
                printf("Feet to Miles!\n");
                break;
            case 7:
                printf("Cups to Quarts!\n");
                break;
            case 8:
                printf("Quarts to Cups!\n");
                break;
            case 9:
                printf("Liters to Quarts\n");
                break;
            case 10:
                printf("Quarts to Liters\n");
        
            }    
    return 0;
    }
    
    any help would be great! ive been stuck for hours!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    output=milesToKilo();
    printf("That would be %f Kilometers!\n", milesToKilo);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In addition to Salem's correction - You're printing the result twice. Either print the message within the function, or after the function has been called. (If you're returning a value from the function, then you probably want the latter.)

    Code:
    float milesToKilo(float input, float output)
    {
        output=0;    
        output=input*1.609344;
        printf("Thats %f kilometers!", output);
        return output;
    }
    
    ...
    
        case 1:
            printf("How many miles?\n");
            scanf("%f", &input);
            output=milesToKilo();            
            printf("That would be %f Kilometers!\n", milesToKilo);
            break;

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    So what exactly is your correction Salem? Thanks matticus, ill remove it from the function :-)

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Sixx View Post
    So what exactly is your correction Salem? Thanks matticus, ill remove it from the function :-)
    Look at the two lines in red in my post #3 above.
    The one inside the function is correct, matching the %f format specifier with the variable "output."
    The one in the switch statement is not correct, as you don't have a variable called "milesToKilo."

    You can use the "milesToKilo()" function (with parenthesis) in the "printf()" statement also.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Hmm, there's more to be said about how you're declaring and using your functions. Perhaps a brush up is in order.
    Functions in C - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with functions
    By saliya in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 10:36 PM
  2. problem with functions
    By ademkiv in forum C Programming
    Replies: 8
    Last Post: 02-27-2006, 11:12 AM
  3. problem with using functions
    By Drakon in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2004, 08:09 PM
  4. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 04:34 PM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM