0

I am fairly new to programming and came upon a following problem: I am getting an error C2124, which means that I am using 0 for division However, as you can see, the number I am using is not zero but a really small number. When I change division numbers to 5/2, I get 2 as a result, as if I am using int variable for x. What am I doing wrong?

#include<iostream>
using namespace std;

int main()
{
    double x = 0.0;
    x = 1/((6/45)*(5/44)*(4/43)*(3/42)*(2/41)*(1/40));
    cout << "The number is : " << x << endl;
    system("pause");
    return 0;
}
PrOfSeS
  • 5
  • 1
  • Because of integer math 6/45 is 0 as well as all of the other fractions so you have 1/0. Remember when both operands are an integer the result is also an integer. – drescherjm Oct 09 '20 at 14:38
  • I am aware that integer can only show whole numbers, but why is it happening in this case? I am using double variable for x. – PrOfSeS Oct 09 '20 at 14:44
  • 6/45 is calculated using integer math and the result is an integer (any fractional part is thrown away because it can't have a fractional part as an integer). It's still an integer even though you want to put the final value of the whole expression in a double. So you have 1 / (0 * 0 * 0*0 *0*0). The solution is make 1 number in each of the fractions be a double. (6.0 / 45) will be calculated as a double instead of an integer. – drescherjm Oct 09 '20 at 14:46
  • @PrOfSeS -- An assignment is first done by evaluating what is on the right of the `=` sign. There are no `double`'s being used in the calculation. – PaulMcKenzie Oct 09 '20 at 14:51
  • As @PaulMcKenzie everything on the right side are integers and calculated as such. – drescherjm Oct 09 '20 at 14:52
  • It is working now, thank you for the help! – PrOfSeS Oct 09 '20 at 17:25

0 Answers0