0

I seem to have this weird issue with my computation result. I seem to always get the result of -0.125 when it should be 1.375. Could it be the way i place my computation?

Below are the .cpp files involved in the computation:

LocationData.cpp
This is the part where the computation is done.

float LocationData::computeCivIndex(string sunType,int planetNo,int moonNo,float particulatePercent,float plasmaPercent) {
float civIndex;
int sunTypePercent; 

if (sunType == "O") 
    sunTypePercent = 30;
if (sunType == "B") 
    sunTypePercent = 45;
if (sunType == "A") 
    sunTypePercent = 60;
if (sunType == "F") 
    sunTypePercent = 75;
if (sunType == "G") 
    sunTypePercent = 90;
if (sunType == "K") 
    sunTypePercent = 80;
if (sunType == "M") 
    sunTypePercent = 70;

cout << sunTypePercent<< endl;
//calculate the civIndex
civIndex = ((sunTypePercent/100) - (particulatePercent+plasmaPercent)/200) * (planetNo+moonNo);
return civIndex;
}

MissionPlan.cpp
This is the part where the computation method is called

void MissionPlan::computeCivIndex() {
LocationData ld;

string type;
int planets,moons;
float particulate,plasma; 

if (db.size() == 0)
    cout << "No data! Please input statistical data before proceeding..." << endl;
else {
    for (vector<PointTwoD>::iterator itr = db.begin(); itr != db.end(); ++itr) {
        ld = itr->getLocationData();
        type = ld.getSunType();
        planets = ld.getNoOfEarthLikePlanets();
        moons = ld.getNoOfEarthLikeMoons();
        particulate = ld.getAveParticulateDensity();
        plasma = ld.getAvePlasmaDensity();

        //i print out the values to check that it is the values i inputted beforehand(manually)
        cout << type << endl;
        cout << planets << endl;
        cout << moons << endl;
        cout << particulate << endl;
        cout << plasma << endl;

        itr->setCivIndex(ld.computeCivIndex(type,planets,moons,particulate,plasma));
    }
}
cout << "Computation complete! (" << db.size() <<  " records were updated)" << endl;
}

Any ideas on how to fix this weird issue? Thanks!

4

2 回答 2

4

(sunTypePercent/100) is always zero when sunTypePercent is an int less than 100, because you're doing integer division.

You probably need sunTypePercent to be a float (or double).

于 2013-10-20T07:56:13.960 回答
2

Change

int sunTypePercent;

to

float sunTypePercent;

OR:

civIndex = ((sunTypePercent/100.0) - (particulatePercent+plasmaPercent)/200.0) * (planetNo+moonNo);
于 2013-10-20T08:00:01.523 回答