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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-24-2010
beams75
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
• 02-24-2010
NeonBlack
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.
• 02-24-2010
beams75
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?
• 02-24-2010
prog-bman
What compiler are you using?
• 02-24-2010
beams75
microsoft visual studio 2008
• 02-24-2010
NeonBlack
I am suggesting that you read in the values else where (say in the main function) and pass them into the two functions.
• 02-25-2010
Quote:

Originally Posted by beams75
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).
• 02-25-2010
beams75
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
• 02-25-2010
MK27
:p

This idiot will be working on some future "brake pedal" projects, no doubt.
• 02-25-2010
Quote:

Originally Posted by beams75
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?
• 02-25-2010
rags_to_riches
Yeah, good luck with that.
• 02-25-2010
_Mike
Quote:

Originally Posted by beams75
how bout you dont help me then?

You're not even asking for help. You're asking to be spoonfed the solution..
• 02-25-2010
wtaplin
Quote:

Originally Posted by beams75
im an engineering major

I didn't know special ed kids could get into engineering programs. What a D-bag.
• 02-25-2010
MK27
Quote:

Originally Posted by wtaplin
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".
• 02-25-2010
wtaplin
Quote:

Originally Posted by mk27
they can if they want to be "real engineers".

rofl :D
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last