I'm trying to return a value from a function but all it return is the value 1.
It's suppose to have 5 arguments in the computeCivIndex()
, even if I hard coded the values the output I receive would still be 1.
Why is this so ?
float LocationData::computeCivIndex()
{
civNum1 = 45.0 / 100;
civNum2 = 20 + 50;
civNum3 = civNum2 / 200;
civNum4 = civNum1 - civNum3;
civNum5 = 5 + 10;
return civNum;
}
//display data
void LocationData::displaydata()
{
cout << "CIV value: " << computeCivIndex << endl;
}
You miss ()
in cout << "CIV value: " << computeCivIndex() << endl;
. For importance of braces you can check this link .
cout << "CIV value: " << computeCivIndex << endl;
seems to be printing the value of the function (not the return value). You need to put the function brackets in:
cout << "CIV value: " << computeCivIndex() << endl;
//convert sunType to sunTypePercentage
float LocationData::computeCivIndex(string st, int earth, int moons, float particle, float plasma)
{
float sunTypePercent;
if(st == "Type 0")
{
sunTypePercent = 80.0;
}
else if(st == "Type B")
{
sunTypePercent = 45.0;
}
else if(st == "Type A")
{
sunTypePercent = 60.0;
}
else if(st == "Type F")
{
sunTypePercent = 75.0;
}
else if(st == "Type G")
{
sunTypePercent = 90.0;
}
else if(st == "Type K")
{
sunTypePercent = 80.0;
}
else if(st == "Type M")
{
sunTypePercent = 70.0;
}
// calculate CIV Value
float civNum,civNum1,civNum2,civNum3,civNum4,civNum5;
civNum1 = sunTypePercent / 100;
civNum2 = plasma + particle;
civNum3 = civNum2 / 200;
civNum4 = civNum1 - civNum3;
civNum5 = earth + moons;
civNum = civNum4 * civNum5;
return civNum;
}
//display data
void LocationData::displaydata()
{
cout << "suntype: " << sunType << endl;
cout << "earth: " << noOfEarthLikePlanets << endl;
cout << "moons: " << noOfEarthLikeMoons << endl;
cout << "particle: " << aveParticulateDensity << endl;
cout <<"density: " << avePlasmaDensity << endl;
cout << "CIV value: " << computeCivIndex()<< endl;
}
This is the actual code which i'm having problem with. For the computeCivIndex() function it is actually a static under the public LocationData class in my LocationData.h file which look something like this.
static float compute CivIndex(string st, int earth, int moons, float particle, float plasma);
so in order to retrieve the value from the function should I do this instead?
cout << "CIV value: " << LocationData.computeCivIndex() << endl;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.