Jun 8, 2014 at 4:55pm UTC
How do I return a specific value from a function in order for me to cout it in my int main?
In this case i have solved for a value sigma1 in my function but i want to use the value of sigma1 in my int main also and in other places so how would i do that?
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;
bool isInt (double value) {
double dummy;
return bool(modf(value, &dummy) == 0);
}
double sqr(double value) {
return value * value;
}
double computeSolidity(double B1,double B2);
int main (void) {
double B1;
double B2;
double I;
double conditionB1;
double alpha1;
for (;;){
cout << "Enter Flow Entry Angle , Flow Exit Angle , and Incidence (-1 -1 -1 to exit) " << endl;
cout << endl;
cin >> B1 >> B2 >> I;
cout << endl;
if ( B1 == -1 && B2 == -1 && I == -1){
break;
}else{
conditionB1 = (36-(0.45*B2))/(B1-B2);
if(B2 <= -10 || B2 >= 50 || I <= -3 || I >=3 || conditionB1 >= 1.25 || conditionB1 <= 0.75 ){
cout << "Error ! Invalid Values Ignored " << endl;
}else{
alpha1 = B1 - I;
computeSolidity(B1,B2);
cout << endl;
cout << "Blade Entry Angle : " << alpha1 << endl;
cout << "Solidity : " << sigma1 << endl; // how to get sigma 1??
}
}
}
system("PAUSE"); return 0;
}
double computeSolidity(double B1,double B2){
double LHS;
double sigma = 0.6;
double sigma1;
double LHScount;
double LHScount1;
cout << "Solidity" << setw(20) << "LHS(Value)" << endl;
cout <<"------------------------------" << endl ;
for(sigma; sigma<2.2 ;sigma+=0.1){
LHS = 33.5291 + (0.469188 + 0.0020961*B2)*B2 - B1 + (0.187148*B2-15.2599)*log(1/sigma)-0.677212*(pow(log(1/sigma),2));
if(sigma==0.6){
LHScount1 = abs(LHS);
}else{
LHScount = abs(LHS);
if( LHScount < LHScount1){
LHScount1 = LHScount;
sigma1=sigma;
}
}
cout << setiosflags (ios::showpoint|ios::fixed);
cout <<setw(5) << setprecision(1)<< sigma << setw(20) << setprecision(4) <<LHS << endl;
}
cout << "Leaving Function The chosen value is " << setprecision(1) << sigma1 << endl;
return sigma1;
}
Last edited on Jun 8, 2014 at 4:56pm UTC
Jun 8, 2014 at 4:59pm UTC
If you want to use the return value of a function later on you will have to store it in a variable.
double sigma1 = computeSolidity(B1,B2);
Note that this variable is a different variable than sigma1 inside the computeSolidity function so you don't have to use the same name if you don't want to.
Last edited on Jun 8, 2014 at 5:00pm UTC
Jun 8, 2014 at 5:03pm UTC
Where would I declare it though plus i dont think my function is returning sigma1 for some odd reason it doesnt return anything
Jun 8, 2014 at 5:14pm UTC
That doesn't sound like the same problem. There's a big difference between a function failing to return a value, and the entire program crashing.
Jun 8, 2014 at 5:17pm UTC
Ok i have fixed the issue i was computing the function twice and thats why the program was crashing thanks alot for your help guys :) appreciate it