0

My C++ program takes in three integer inputs and does some computations including division. At first, the end result was not a decimal. After searching a little on Stack Overflow (Dividing two integers to produce a float result), I found out that I'm supposed to add a (float), like this:

int a;
int b;
float c;

a = 5;
b = 2;

c = (float)a / b;

So I tried that but I ended up with my result being -nan(ind). First off, what does that mean? And second, how do I make my program work?

Here is the math I am trying to do in my program by the way.

int v;
int e;
int a;
float r;

r = ((float)e - v) / ((float)a / 1000);

Edit: Sorry, my mistake. I meant to type 1000 in the second example, not c.

Superrbo
  • 9
  • 1
  • 1
  • 3
  • what do you get when you `std::cout << c` in your first example? – PYA Jul 05 '17 at 14:23
  • 1
    Can't reproduce, `c` is 2.5 for me. The problem may be in how you check the value of `c`. – François Andrieux Jul 05 '17 at 14:26
  • 3
    There's nothing in the first code sample you've shown that would do this; the second code sample is nonsense, because it uses uninitialized variables. Provide the smallest code sample you can come up with that **compiles, runs, and shows the problem**. – Pete Becker Jul 05 '17 at 14:26
  • It means that c is not a number. So either you've printed it out wrongly or your code is not the code you posted (or the compiler is bugged, which we can rule out). – Malcolm McLean Jul 05 '17 at 14:41

2 Answers2

2

Try converting each variable to float, and see if that fixes the problem. Also, in your second example, you never initialize your variables, which causes an error because their value is undefined, it could be anything. You also forgot to include the variable c.

See if this throws an error:

int v = 1;
int e = 1;
int a = 1;
int c = 1;
float r;

r = ((float)e - (float)v) / (float)a / (float)c;

Also, for converting between types, I would recommend you use 'static_cast<type>(variable)' instead of '(type)variable'. It is the recommended way to cast types in c++, and it also makes conversions easier to spot when debugging. Your code would look like this:

int v = 1;
int e = 1;
int a = 1;
int c = 1;
float r;

r = (static_cast<float>(e) - static_cast<float>(v)) / static_cast<float>(a) / static_cast<float>(c);
Misha
  • 73
  • 1
  • 6
  • Note that you can even use float r= .... for the last declaration and assignment. – Hans Olsson Jul 05 '17 at 14:38
  • @HansOlsson that's true! Although I expect them to have code to get the values for v, e, a, and c between the declaration of the values and the last line. – Misha Jul 05 '17 at 14:53
1

You have to initialize every variables like your first example and also assign values of each and every variables.
Try this :

int v;
int e;
int a;
int c;
float r;



a = 5;
c = 2;
e = 5;
v = 2;

r = ((float)e - v) / (float)a / c;
cout << r;
Minar Mnr
  • 1,376
  • 1
  • 16
  • 23
  • 1
    While this might answer the problem including an explanation of why/how this code is an answer would make it better. – NathanOliver Jul 05 '17 at 14:30