Search code examples
c++logarithm

Delta V Calculator issue with Logarithms


I'm attempting to create a program to figure out Delta-V for my Kerbal Space Program game, and C++ (being run in the Eclipse IDE) seems to believe that my attempt to call the "log()" function is actually me referencing an uncreated function. I highly appreciate any help in the matter!

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    cout << "Hello. Welcome to the Kerbal Space Program Delta V Calculator. \n";
    cout << " \n";
    cout << "Note that each stage must use the same engine for this calculator.";
    cout << "\n";
    cout << "\nHow many stages make up your rocket? :";
    int stageNumber;
    cin >> stageNumber;
    //cout << "Your rocket has " << stageNumber << " stages.\n";
    cout << "\n\nStart from the bottom stage, please. ";
    //ACTUAL DELTA V CALCULATIONS
    for(int currentStage = 1; currentStage <= stageNumber; currentStage = currentStage + 1){
        cout << "What is the total mass of this stage? :";
        int totalMass;
        cin >> totalMass;
        cout << "What is the fuel mass of this stage? :";
        int fuelMass;
        cin >> fuelMass;
        cout << "\n";
        int dryMass;
        dryMass = totalMass - fuelMass;
        cout << "Your dry mass is" << dryMass << "\n";
        cout << "\n";
        cout << "Give the specific impulse of this stage's engine. \n";
        int iSP;
        cin >> iSP;
        cout << "Here is the Delta V of your rocket.\n";
        int deltaMass;
        deltaMass = totalMass/dryMass;
        int deltaV;
        deltaV = iSP * log(deltaMass);
        cout << deltaV;

        exit(0);
    }

}

Solution

  • I think for your calculations you want to use double (which I would prefer here) or float variables. Now all decimal places are truncated as you do integer division. Also your loop will not do more than one iteration, as exit(0); is called which terminates the whole program. Remove it and exit your main function with return 0;. Your error message happens because there is no log(int) in cmath. This would also resolved by using double. If you get linker errors append linker option -lm.