Thread: Please Help....error problems

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Please Help....error problems

    Help! I am writing this code and it is kicking my butt! I have reread the book and made every change I can think of...I am LOST! Any advise would be greatly appreciated!

    This is the assignment:

    Write a MAIN function and the following funcations to compute the stress and strain in a steel rod of diameter D (inches) and length L (inches) subject to the compress loads P of 10,000 to 1,000,000 pounds in increments of 100,000 pounds. The modulus of elasticity E for steel is 30 x 10 to the 6th power.


    A function to compute the stress from the formulas:

    stress f = P/A

    Where A = PI Dsquared/4.0

    A funcation to compute the strain from the formulas:

    elondated or shortened length (delta)L=fL/E

    strain e = (delta)L/L =f/e

    A funcation to output the stress and strain at differant loads of P

    Main
    *
    *****************************
    * * *
    calculate stress calculate strain output

    The functions should call each other as shown in the chart above.


    Here is the code I wrote and am having problems with:

    Code:
    #include<stdio.h>
    #include <math.h>
    float compute_stress();
    float compute_strain();  
    float output_results();
    
    
    	int main( )
    {
    
    	
    	float compute_stress (float stress, int p);
    	float compute_strain (float strain);	
    	void output_results(float stress, float strain, int p);
    
    	
        return 0;
    }  
    float compute_stress()
    {
    	const float PI = 3.141593;
        float stress, diam , area;
    
    	printf("Enter diameter:  \n");
    	scanf_s("%d", &diam);
    
    	
    	area=(PI * (diam *diam ))/4;
    
    	{
    		int p;
    		for(p=10000; p<=100000000; p= p+100000)
    		{
    	stress = p/area;
    	
    
        return stress;
    		}
    	}
    }
    
    float compute_strain(float stress)	
        {
    		const float e=30000000;
    	    float strain;
    
    	    strain = stress/e;
    	
    	    return strain;
        
    	
    }
    void output_results (int p, float stress, float strain)
    {	
    	
        printf("Compression Load = %f lbs.     Stress = %f      Strain = %f", p, stress, strain);
    
    	return;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what errors are you having? Can you just divide everything by 1000 or so, since you're starting out that high?


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

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    I am still VERY new to this so it is all pretty confusing for me! I did not mess with the amounts as they are what the assignment calls for.

    These are the messages it is giving me:

    Warning 1 warning C4305: 'initializing' : truncation from 'double' to 'const float'

    Warning 2 warning C4715: 'compute_stress' : not all control paths return a value

    Plus when it runs it does NOT ask me to input the diameter. For the life of me I can not figure out what I am doing wrong. Any help you can offer would be great! I have been beating my head against the wall for a week now...ugh!!!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. doubles are big floats. So any time you assign the value from a double to a float, you'll get a warning.

    2. Use code tags, and better indentation:
    Code:
    float compute_stress()
    {
        const float PI = 3.141593;
        float stress, diam , area;
    
        printf("Enter diameter:  \n");
        scanf_s("%d", &diam);
    	
        area=(PI * (diam *diam ))/4;
    
        { /* why is this here */
            int p;
            for(p=10000; p<=100000000; p= p+100000)
            {
                stress = p/area;
                return stress;
            }
        }
        /* if you reach here, there's nothing returning */
    }

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

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    I tried removing them and that does noy seem to help either. They were there because everywhere the command directly below them was used so were they. Sorry, still trying to figure this all out.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, look at that loop. Why even have a loop if all you do is return on your first pass through it?

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

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Thanks,
    I will try to figure out how to get it to repeat. I appreciate the direction.

Popular pages Recent additions subscribe to a feed