Thread: how to use data from one function in another...?

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

    how to use data from one function in another...?

    hey first time here. im a college student and im stuck in my computer science class on this program right now. i have to test if three values for the sides of a triangle can actually equal a triangle with this code:

    Code:
    double is_triangle();
    
    double is_triangle()
    {
    	double t1;
    	double t2;
    	double t3;
    	double total1;
    	double total2;
    	double total3;
    	
    	cout<<"Side 1: ";
    	cin>>t1;
    	while(t1<=0)
    	{
    		cout<<"Side lengths must be positive. Try again."<<endl;
    		cout<<"Side 1: ";
    		cin>>t1;
    		cout<<endl;
    	}
    
    	cout<<"Side 2: ";
    	cin>>t2;
    	while(t2<=0)
    	{
    		cout<<"Side lengths must be positive. Try again."<<endl;
    		cout<<"Side 2: "; 
    		cin>>t2;
    		cout<<endl;
    	}
    
    	cout<<"Side 3: ";
    	cin>>t3;
    	while(t3<=0)
    	{
    		cout<<"Side lengths must be positive. Try again."<<endl;
    		cout<<"Side 3: ";
    		cin>>t3;
    		cout<<endl;
    	}
    
    	total1=t1+t2;
    	total2=t2+t3;
    	total3=t1+t3;
    
    	if ((total1>t3)&&(total2>t1)&&(total3>t2))
    	{
    		cout<<"The values of the sides equal a triangle."<<endl;
    		cout<<endl;
    	}
    	else
    	{
    		cout<<"The values of the sides do not equal a triangle."<<endl;
    		cout<<endl;
    	}
    	
    	return(0);
    }
    and im trying to find the area with another function using Heron's Method, which requires the three values for the sides. im not allowed to ask for the sides again so i need to use the t1, t2, and t3 values from the above function. how do i do that? here is my area code:

    Code:
    double tri_area();
    
    double tri_area()
    {
    	double s;
    	double area;
    	double t1;
    	double t2;
    	double t3;
    
    	s=(t1+t2+t3)/2;
    	area=sqrt(s*(s-t1)*(s-t2)*(s-t3));
    
    	cout<<"The area of the triangle is "<<area<<" units^2."<<endl;
    	cout<<endl;
    	
    	return(area);
    }
    any input would really be appreciated

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    A suggestion: make the function signatures
    Code:
    bool is_triangle(double side1, double side2, double side3);
    And
    Code:
    double tri_area(double side1, double side2, double side3);
    That might help.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    it didnt pick up the bool identifier, and its still saying that t1,t2,t3 are uninitialized in the second function. do i need to #include another library for bool?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    What compiler are you using?
    Woop?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    microsoft visual studio 2008

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I am suggesting that you read in the values else where (say in the main function) and pass them into the two functions.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by beams75 View Post
    it didnt pick up the bool identifier, and its still saying that t1,t2,t3 are uninitialized in the second function. do i need to #include another library for bool?
    NeonBlack suggested you create the function prototypes and those include the arguments being passed to it, namely "side1", "side2", etc. Did you just (i.e. blindly) copy and paste the function prototypes? Hopefully not. In the "tri_area" function you have
    Code:
        double s;
        double area;
        double t1;
        double t2;
        double t3;
    
        s=(t1+t2+t3)/2;
    As the compiler is telling you, you are using t1, t2, t3 without having initialized them first. That is, you're using them without giving them a value first, which means the result of the "s=..." line is more or less useless. If you're using the function prototype suggested by NeonBlack, then you should be working on the arguments passed to the function, not these "t1", "t2", etc. These 3 variables aren't needed anymore, because they'll be given to the function as arguments when its called, so you dont manually create and assign them values.

    Also, the "bool" type is a built in type just like "int", etc, so it should be working fine. Maybe your misinterpreting the error/warning. If so, post all of your code and all of the error/warning message(s). Otherwise we can only guess and waste our time (which I for one wont do).

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    how bout you dont help me then? i knew this would happen if i came on some computer nerd site, ill just ask my damn teacher instead. its not like i give a damn about this anyway, im an engineering major, not some fat, antisocial computer bum. so shut the ........ up. ill never be back so post what you want about me

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300


    This idiot will be working on some future "brake pedal" projects, no doubt.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by beams75 View Post
    how bout you dont help me then? i knew this would happen if i came on some computer nerd site, ill just ask my damn teacher instead. its not like i give a damn about this anyway, im an engineering major, not some fat, antisocial computer bum. so shut the ........ up. ill never be back so post what you want about me
    Are you talking to me? Was it my reply, with the intention of only helping, that inclined you to say this?

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yeah, good luck with that.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by beams75 View Post
    how bout you dont help me then?
    You're not even asking for help. You're asking to be spoonfed the solution..

  13. #13
    Registered User wtaplin's Avatar
    Join Date
    Dec 2009
    Posts
    13
    Quote Originally Posted by beams75 View Post
    im an engineering major
    I didn't know special ed kids could get into engineering programs. What a D-bag.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by wtaplin View Post
    I didn't know special ed kids could get into engineering programs. What a D-bag.
    They can if they want to be "real engineers".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User wtaplin's Avatar
    Join Date
    Dec 2009
    Posts
    13
    Quote Originally Posted by mk27 View Post
    they can if they want to be "real engineers".
    rofl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM