Thread: function problem...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    function problem...

    I think I asked this before, but can't find it in my posts. Feel like an idiot asking it again.

    Anyways there are two if()'s. The first one is to convert degrees F to C through a function. I have already done it without functions as shown in the second if().

    The problem is that the function keeps sending tempF to be 0...it seems. Tried reading it out loud what it does but the program makes sense in my head. Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    float tempC;
    float tempF;
    
    float calcCelsius1(tempF);
    
    int main()
    {
        int choice;
    
        float start_temp;
        float end_temp;
        float temp_incr;
    
    
    
    
        printf("Which Temperature Scale Conversion would you like to perform?\n");
        printf("1. Convert F to C\n2. Convert C to F\n\n");
    
        printf("What is your choice? ");
        scanf("%d", &choice);
    
        if (choice == 1)
        {
            printf("\nStarting Temperature: ");
            scanf("%f", &start_temp);
    
            printf("Ending Temperature: ");
            scanf("%f", &end_temp);
    
            printf("Temperature Increment: ");
            scanf("%f", &temp_incr);
    
    
            tempF = start_temp;
            printf("\n\nFahrenheit\t\tCelsius\n");
            printf("----------\t\t-------\n");
    
            while(tempF<end_temp)
            {
                tempC = calcCelsius1(tempF);
                printf("TempC = %f\n", tempC);
                printf("%6.1f\t\t\t%6.1f\n", tempF, tempC);
                tempF = tempF + temp_incr;
            }
            tempF = end_temp;
            tempC = calcCelsius1(tempF);
            printf("%6.1f\t\t\t%6.1f\n", tempF, tempC);
    
        }
    
        else if (choice == 2)
        {
    
            printf("\nStarting Temperature: ");
            scanf("%f", &start_temp);
    
            printf("Ending Temperature: ");
            scanf("%f", &end_temp);
    
            printf("Temperature Increment: ");
            scanf("%f", &temp_incr);
            tempC = start_temp;
            printf("\n\nCelsius\t\t\tFahrenheit\n");
            printf("-------\t\t\t----------\n");
    
            while(tempC<end_temp)
            {
                tempF =  tempC*9/5 + 32;
                printf("%6.1f\t\t\t%6.1f\n", tempC, tempF);
                tempC = tempC + temp_incr;
            }
            tempC = end_temp;
            tempF = tempC*9/5 + 32;
            printf("%6.1f\t\t\t%6.1f\n", tempC, tempF);
        }
    
        else
            printf("Not a valid choice.\n");
    
    
        return 0;
    }
    
    float calcCelsius1 (tempF)
    {
        tempC = (tempF - 32)*5/9;
        return tempC;
    }
    I finally got to fit a C++ class in my schedule for spring. So hopefully formal education on the subject will stop me coming here (asking questions that is)
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    float tempC;
    float tempF;
     
    float calcCelsius1(tempF);
    1. You are using global variables, and as such, you don't need to pass them as arguments to a function. I'm not saying that's the best way to do it, but you don't need function arguments if you are trying to reference globals.

    2. 'tempF' as a function parameter does not have a type. I would be surprised if this actually compiled. If it did, it's because it decided to default to an int. This gives you another problem, which is that you are now using a local variable called 'tempF', and as such, you aren't using your global anymore.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    That was the problem

    As always thanks for your help!
    My Ctrl+S addiction gets in the way when using Code Blocks...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    tempF =  tempC*9/5 + 32;
    
    ...
    
    tempC = (tempF - 32)*5/9;
    Anther problem you might have is integer division. Such computations will not provide you the results you think you should be getting.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In both of those, the multiplication should happen before the division, and the multiplication involves a float. So in theory that should be okay.
    I don't think the compiler is allowed to pre-calculate 9/5 as 1 in this case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  3. function inside function (most likely) problem
    By vlaskiz in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2011, 05:10 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM