Thread: Please someone help

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    12

    Exclamation Please someone help-One more question

    I am new to programming and am in a class in college. I have written this program to convert to the provided units. My output data is all screwed up and I have no idea why. This is due tomorrow so any help is greatly appreciated.
    Code:
    // Name: John King
    // Date: Monday February 15 at 8:oo AM
    // Desc: The purpose of this project is to modularize our Project One. Also to $
    //       conversions in for Kelvi and inches.
    
    #include <stdio.h>
    
    //Prototypes
    
    void getInput(int* pFahrenheit, int* pFeet, int* pPounds);
    int calc(int fahrenheit, int feet, int pounds);
    float calcTemp(int fahrenheit);
    float calcDistance(int feet);
    float calcWeight(int pounds);
    void display (int fahrenheit, int celsius, int kelvin, int feet, int meters, in$
                  int pounds, int kilograms);
    
    //calls all the other functions
    int main(void)
    {
       int fahrenheit, feet, pounds;
       int celsius, kelvin, meters, inches, kilograms;
       
       getInput(&fahrenheit, &feet, &pounds);
       calc(fahrenheit, feet, pounds);
       display(fahrenheit, celsius, kelvin, feet, meters, inches, pounds, kilograms$
       
       return 0;
    
    }
     
    void getInput(int* pFahrenheit, int* pFeet, int* pPounds)
    {
       printf("\nName: John King");
       printf("\nPlease enter an integer value for Fahrenheit:");
       scanf("%d", pFahrenheit);
       printf("\nPlease enter an integer value for Feet:");
       scanf("%d", pFeet);
       printf("\nPlease enter an integer value for Pounds:");
     scanf("%d", pPounds);
     
    }
    
    int calc(int fahrenheit, int feet, int pounds)
    {
       
       calcTemp(fahrenheit);
       calcDistance(feet);
       calcWeight(pounds);
    
    }
     
    float calcTemp(int fahrenheit)
    {
       float celsius, kelvin;
       
       celsius = (fahrenheit - 32) / 1.8;
       kelvin = celsius + 273.15;
       
    }  
     
    float calcDistance(int feet)
    {
       float meters, inches;
       
       meters = feet / 3.2808399;
       inches= feet * 12;
       
    }  
    
    float calcWeight(int pounds)
    {
       float kilograms;
       
       kilograms = pounds / 2.20462262;
       
       
    }  
       
    void display(int fahrenheit, int celsius, int kelvin, int feet, int meters, int$
                 int pounds, int kilograms)
    {
       printf("\n%20s%20s%20s%20s%20s%20s", "Original", "Value", "Converted To",
               "Value", "Converted To", "Value");
       printf("\n%20s%20s%20s%20s%20s%20s", "--------", "-----", "------------", "-$
                "------------", "-----");
       printf("\n%20s%20d%20s%20.3d%20s%20.3d", "Fahrenheit", fahrenheit, "Celsius"$
               "Kelvin", kelvin);
       printf("\n%20s%20d%20s%20.3d%20s%20.3d", "Feet", feet, "Meters", meters, "In$
      printf("\n%20s%20d%20s%20.3d", "Pounds", pounds, "Kilograms", kilograms);
       printf("\n\n");
    }
    Last edited by jsking09; 02-14-2010 at 08:41 PM. Reason: need more help

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    Your code to calculate the temperatures and to receive input is ok, but the problem is that at the moment that you make the calculations, you don't change the variables that you use to display.
    Besides that, those variables must be in the same type as in the calc function (in this case you have them in type int on main and type float on the calc functions).

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I think the problem is your variable scope. Remember that when you declare a variable inside a function, it exists inside that function, and ceases to exist when the function returns. So if you have a variable 'a' in your main() function, and you call another function that declares a variable 'a', they are two entirely separate variables. Which one you are referring to depends where you are (or, what 'scope' you are in). You obviously know you can pass pointers so that functions refer to the same variable - but you can also declare variables globally - outside of any function, so that it's accessible from every function. It is considered good style to minimize the number of global variables.

    So what's happening is the values that you calculate are lost because those variables cease to exist. Instead, when you try printing those values, you're referring to the variables you declared in your main function - and their values are, for all intents and purposes, random (they just hold the value that happened to be in that part of memory when your program started.)

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    As sean mentioned, the problem is the variable scope. Two possible solutions for this are using either global variables or pointers, exactly the same way you did with the input function.
    An example of that would be:
    Code:
    float calcTemp(int fahrenheit, float *celsius, float *kelvin)
    {
       *celsius = (fahrenheit - 32) / 1.8;
       *kelvin = *celsius + 273.15;
       
    }
    Remember though, to set the types in the main function from int to float, so there aren't any conflicts.
    Last edited by Cynary; 02-14-2010 at 05:27 PM.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    Thanks for the help everyone. I made the corrections and now i am getting many compiler errors that say incompatible type. Once agian thank you I know I look like a complete dunce.

    Code:
     
    // Name: John King
    // Date: Monday February 15 at 8:oo AM
    // Desc: The purpose of this project is to modularize our Project One. Also to add
    //       conversions in for Kelvi and inches.
       
    #include <stdio.h>
       
    //Prototypes
    
    void getInput(int* pFahrenheit, int* pFeet, int* pPounds);
    int calc(int fahrenheit, int feet, int pounds, float *celsius, float *kelvin, float
             *meters, float *inches, float *kilograms);
    float calcTemp(int fahrenheit, float *celsius, float *kelvin);
    float calcDistance(int feet, float *meters, float *inches);
    float calcWeight(int pounds, float *kilograms);
    void display (int fahrenheit, float celsius, float kelvin, int feet, float
    meters, float inches, int pounds, float kilograms);
       
    
    //calls all the other functions
    int main(void)
    {
       int fahrenheit, feet, pounds;
       float *celsius, *kelvin, *meters, *inches, *kilograms;
    
       getInput(&fahrenheit, &feet, &pounds);   
       calc(fahrenheit, feet, pounds, *celsius, *kelvin, *meters, *inches, *kilograms);
       display(fahrenheit, *celsius, *kelvin, feet, *meters, *inches, pounds, *kilograms);
    
       return 0;
    }
    
    void getInput(int* pFahrenheit, int* pFeet, int* pPounds)
    {             
       printf("\nName: John King");
       printf("\nPlease enter an integer value for Fahrenheit:");
       scanf("%d", pFahrenheit);
       printf("\nPlease enter an integer value for Feet:");
       scanf("%d", pFeet);
       printf("\nPlease enter an integer value for Pounds:"); 
       scanf("%d", pPounds);
               
    }
    
    int calc(int fahrenheit, int feet, int pounds, float *celsius, float*kelvin, float
             *meters, float *inches, float *kilograms)
    {
    
       calcTemp(fahrenheit, *celsius, *kelvin );
       calcDistance(feet, *meters, *inches);
       calcWeight(pounds, *kilograms); 
    
    }
     
    float calcTemp(int fahrenheit, float *celsius, float *kelvin)
    {
                  
       *celsius = (fahrenheit - 32) / 1.8;
       *kelvin = *celsius + 273.15;
       
    }
       
    float calcDistance(int feet, float *meters, float *inches)
    {
               
       *meters = feet / 3.2808399;
       *inches= feet * 12;
    
    }
     
    float calcWeight(int pounds, float *kilograms)
    {
       
      *kilograms = pounds / 2.20462262;
    
     
    }
    
    void display(int fahrenheit, float celsius, float kelvin, int feet, float meters, float
                  inches, int pounds, float kilograms)
       
    {
       printf("\n%20s%20s%20s%20s%20s%20s", "Original", "Value", "Converted To",
               "Value", "Converted To", "Value");
       printf("\n%20s%20s%20s%20s%20s%20s", "--------", "-----", "------------", "-----",
                "------------", "-----");
       printf("\n%20s%20d%20s%20.3d%20s%20.3d", "Fahrenheit", fahrenheit, "Celsius", celsius,
               "Kelvin", kelvin);
       printf("\n%20s%20d%20s%20.3d%20s%20.3d", "Feet", feet printf("\n%20s%20d%20s%20.3d", "Pounds", pounds, "Kilograms", kilograms);
       printf("\n\n");
    }

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    I'm not very good with pointers, so I'm going to try and help with some of the other stuff first.

    nevermind what I had here earlier, I wasn't taught to use %s like you were

    You have several function's with return types, yet you are not returning anything. This will also cause you several errors.

    for example, it should look like:

    Code:
    
    int getInteger(); //prototype
    
    int main()
    {
       int x;
       x = getInteger()  //I'm calling getInteger, note: x is an int, and the return type of getInteger is int
                                 //the type of x and the return type must be the same
       printf("x = %i\n", x)
    
       return 0;
    }
    
    int getInteger()
    {
         int y;
         scanf("%i", &y)
         return y;
    }
    Note: I'm still reading your code, I'll edit this when I figure out what else is wrong.

    FYI: I'll be up pretty late. I already have quite a bit of your program working, but I'm not going to just post the code. The more you work on this project, the more help you will get.
    Last edited by madmax2006; 02-14-2010 at 11:54 PM.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Biggest problem I see with your code is that the calc functions expects pointer to float but you are passing in regular floats.

Popular pages Recent additions subscribe to a feed