Thread: Help please

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

    Exclamation Help please

    Someone please tell me what is wrong with this. I have been working on this for 13 hours and I am at wits end.

    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 Kelvin and inches.
    
    #include <stdio.h>
    
    //Prototypes
    
    void getInput(int* pFahrenheit, int* pFeet, int* pPounds);
    int calc(int* pCelsius, int* pKelvin, int* pMeters, int* pInches, int* pKilograms);
    float calcTemp(int fahrenheit, int* pCelsius, int* pKelvin);
    float calcDistance(int feet, int* pMeters, int* pInches);
    float calcWeight(int pounds);
    void display (int fahrenheit, int celsius, int kelvin, int feet, int
                  meters, int inches, 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(&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\n");
       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* pCelcius, int* pKelvin, int* pMeters, int* pInches, int* pKilograms)
    
    {
    
       int fahrenheit, feet, pounds;
       int celsius, kelvin, meters, inches, kilograms;
       
       calcTemp(fahrenheit, &celsius, &kelvin );
       calcDistance(feet, &meters, &inches);
       calcWeight(pounds);
    
    }
     
    float calcTemp(int fahrenheit, int* pCelsius, int* pKelvin)
    {
       
       *pCelsius = (fahrenheit - 32) / 1.8;
       *pKelvin = *pCelsius + 273.15;
       
       return *pCelsius, *pKelvin;
    
    }
    
    float calcDistance(int feet, int* pMeters, int* pInches)
    {  
     
    
       *pMeters = feet / 3.2808399; 
       *pInches= feet * 12;
       
       return *pMeters, *pInches ;
       
    }
    
    float calcWeight(int pounds)
    {
    
       int* pKilograms;
       
      *pKilograms = pounds / 2.20462262;   
       
       return *pKilograms ;
       
    }
     
    void display(int fahrenheit, int celsius, int kelvin, int feet, int meters, int
                  inches, int pounds, int kilograms)
    
    {
       printf("\n%15s%15s%15s%15s%15s%15s", "Original", "Value", "Converted To",
                "Value", "Converted To", "Value");
       printf("\n%15s%15s%15s%15s%15s%15s", "--------", "-----", "------------", "-----",
                "------------", "-----");
       printf("\n%15s%15d%15s%15.3d%15s%15.3d", "Fahrenheit", fahrenheit, "Celsius", celsius,
                "Kelvin", kelvin);
       printf("\n%15s%15d%15s%15.3d%15s%15.3d", "Feet", feet, "Meters", meters, "Inches",
                 inches);
       printf("\n%15s%15d%15s%15.3d", "Pounds", pounds, "Kilograms", kilograms);
       printf("\n\n");
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    the output values do not come out right. like the data isn't passing between the functions correctly.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    #1 - you aren't returning anything in calc (you have an int return type)

    #2 - you are getting a seg fault

    for #2, you need to trace down the seg fault. I usually start commenting out pieces of the program, preferably starting at the bottom like so:

    Code:
    someFunction()
    {
        getSum();
        //getDifference();
        //getHeight();
        //getWeight();
    }
    ....run it, if there's no seg fault, remove a section of comments
    Code:
    someFunction()
    {
        getSum();
        getDifference();
        //getHeight();
        //getWeight();
    }
    oh! there's a seg-fault, it must be in getDifference (the seg fault may be in more than 1 function)

    #3 - you can only return 1 "thing" at a time.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    its not telling me there is a seg fault?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    Some compilers will not tell you there is a segmentation fault, some will.

    Also: When you call calcWeight, you are returning a number, what do you want calcWeight to become?

    Code:
    int calc(int* pCelcius, int* pKelvin, int* pMeters, int* pInches, int* pKilograms)
    
    {
    
       int fahrenheit, feet, pounds;
       int celsius, kelvin, meters, inches, kilograms;
       
       calcTemp(fahrenheit, &celsius, &kelvin );
       calcDistance(feet, &meters, &inches);
       calcWeight(pounds); //<----- is going to become a number, what do you want to do with this number?  
    
    }
    basically, if calcWeight(pounds) returns 5.334343, you're typing 5.334343; You want to say somerandomnumber = calcWeight(pounds);

    and to top it off, you don't want to store 3.21353 in an int, it's not going to be pretty, put it in a double or something
    Last edited by madmax2006; 02-15-2010 at 03:18 AM.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    I need all of the calc functions to convert the input values to the appropriate output. My professor said the prototype for calcWeight HAD to matchthat so idk. and also idk how to fix a seg fault. sorry i have only been doing this for like four weeks

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    in the debugger for codeblocks your program fails with segfault at line 86, on the line that does the calculation in calcweight function.
    you have piles of pointers flying all over the place, i am not sure they are needed, it seems you think that a functions argument has to be a pointer or something?

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    no i honestly just don't know how to use pointers my teacher is very inadequate, and doesn't explain things well. I just need all the values to flow through the program properly so i will get the right output

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    I sent you a pm asking you the project description, it would be helpful if we could know what the requirements are so we don't screw up your grade

    The biggest thing that would help is how the prototypes are supposed to be set up.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    alright i shot it to you in an email.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    Check your pm's.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    done and replied lol

Popular pages Recent additions subscribe to a feed