simple mathmatic equation doesn't work

Yes I'm a beginner, I'm wondering why this will not give me the correct answer

handleLength = (30*(stinkFactor/barPressure))+((stinkFactor/IDIOTS)*((3*tailLength) + weight));

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))

Ignore the weird variable names..
Please post your code, just by looking at the single line you posted is not possible to find out what is happening.
Hi

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>
#include <string>

using namespace 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.

Hope this will help
Well, I output an echo of all the values determined for the variables, and they all work except the handleLength.
Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

const int 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;
 
}  
Last edited on
division between integers produces an integer result.

That is:

1
2
3
4
int foo = 8;
int bar = 12;

cout << (bar / foo); // outputs 1, not 1.5 


In your case:

 
(stinkFactor / IDIOTS)

stinkFactor and IDIOTS are both integers, so this will give you either 0 (if stinkFactor < 8) or 1 (if stinkFactor is 8, 9, or 10)
I tried to calculate that equation with all values 10 and the result was 70 which is correct.
Change line 34 to
 
handleLength = (stinkFactor / (float)IDIOTS) * (3 * tailLength + weight) + 30 * (stinkFactor / barPressure);


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.

You can verify that
 
handleLength = ((float)stinkFactor / IDIOTS) * (3 * tailLength + weight) + 30 * (stinkFactor / barPressure);

will also work.
Last edited on
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.
@Disch

Sorry, your answer was not there when I posted mine.
Don't be sorry. Everyone gets ninja'd once in a while. ;D
Topic archived. No new replies allowed.