0

I have a problem with displaying and keeping any floating number or double or long or floating type this is the code :

int sd, n;
float d;    
d=n/sd.capacity();

cout << d.toFloat() << endl;
cout << n << " / " << sd.capacity() << " = " << d << endl;

In the output I have d equal to 0 everytime, sd.capacity() and n are never 0, so all the time n is lower than sd.capacity value, d = 0 but it should never 0. The variable d does not contain anything but 0.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • `float` is a primitive, not an object. It has no `toFloat()` member function, or any other member functions! – uckelman Jul 03 '14 at 19:51

1 Answers1

0

int/int results into int. So this does a problem.

You need to have atleast one of the operands as float.

Since, you are telling that n always < than sd.capacity(), so n/sd.capacity() in int division always gives 0.

I suppose you should do,

d= (float)n / (float)sd.capacity();

Does this solve it?

j809
  • 1,499
  • 1
  • 12
  • 22