problem with functions

This is a discussion on problem with functions within the C Programming forums, part of the General Programming Boards category; Hey guys! im having some problems with functions. Just started going over them today in class and i was doing ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,267
    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
    2,267
    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
    2,267
    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, 09:36 PM
  2. problem with functions
    By ademkiv in forum C Programming
    Replies: 8
    Last Post: 02-27-2006, 10:12 AM
  3. problem with using functions
    By Drakon in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2004, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21