Every variable displays the correct value individually, but when I compile and run the code, the output of handleLength just gives me the result for (30*(stinkFactor/barPressure))
Try using the code below and replace the '?' with an appropriate variable then compare the result to the same calculation done on a calculator. I did this myself and it seemed to give the correct answer but retesting it using the correct variables wouldn't hurt.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
{
int handleLength;
int stinkFactor = ?;
int barPressure = ?;
int IDIOTS = ?;
int tailLength = ?;
int weight = ?;
handleLength = (30*(stinkFactor/barPressure))+((stinkFactor/IDIOTS)*((3*tailLength) + weight));
cout<<handleLength;
}
system("pause");
}
I'm fairly new to C++ myself so someone more experienced may be able to point out a different error straight away but this should clear up any possibility of any miscalculation and if this calculation is correct then it could be that your variables are being altered elsewhere in your code prior to the calculation which could be skewing the answer. Without seeing the full code i could not be sure.
#include <iostream>
usingnamespace std;
constint IDIOTS = 8;
int main()
{
//Weight of the opossum
double weight;
//Stinkfactor, for a scale of 1-10, 10 being the worst stink.
int stinkFactor;
//Tail length of the 'possum
double tailLength;
//Barametric Pressure
double barPressure;
//This will be the length of the handle that is needed
double handleLength;
//The actual pitchfork number that is required
short neededFork;
cout << "Hello, welcome to the best pitchfork selecting application ever."<< endl;
cout << "In order to make the best possible pitchfork choice, I need some input from you."<< endl;
cout << "What is the weight of the 'possum, in pounds?"<< endl;
cin >>weight;
cout << "On a scale of 1-10(10 being really bad) how bad is the stink?"<<endl;
cin >>stinkFactor;
cout << "We're getting closer. Next, please enter the length of the animal's tail."<<endl;
cin >>tailLength;
cout << "Great, just one last question. What is the current barometric pressure?"<<endl;
cin >>barPressure;
//Calculation for needed handle length
handleLength = (stinkFactor / IDIOTS) * (3 * tailLength + weight) + 30 * (stinkFactor / barPressure);
neededFork = handleLength/10;
//Echo of all input values
cout <<" This is the information you've given me:\n";
cout <<" Weight: "<<weight<< endl;
cout <<" Stink Factor: "<<stinkFactor<< endl;
cout <<" Tail Length: "<<tailLength<< endl;
cout <<" Barametric Pressure: "<<barPressure<< endl;
cout <<" The current IDIOTS score is "<<IDIOTS<< endl;
//Final Calculations
cout <<"This means you you need a pitchfork with a handle of "<<handleLength<<" inches."<< endl;
cout <<"Pitchfork number "<<neededFork<<" will get the job done for you!"<< endl;
}
What happen is stinkFactor and IDIOTS are integers, if IDIOTS>stinkFactor the result of stinkFactor / IDIOTS will be zero (because it is a division between integers). casting IDIOTS to float will make the program to execute a float point division.
Thank you Disch, that was the problem. I can't beleive I didn't notice that. I knew it would upgrade it to a double or whatever automatically.. if one of them was. Didn't realize I had 2 int's dividing i guess.