1

I am getting an illegal use of floating point error in my programme:

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
 clrscr();

 int number,reverse,check,i,j,k=0,x;

 cout<<"Please enter number: ";
 cin>>number;

 //Obtaining no. of digits:
 for(i=1;check==0;i++)
 {
  check/=10;
 }

 //Reversing number:
 if(i%2==0)    //case even digits
 {  for(j=i;j>0;j--)
    {

    x=(number%pow(10,j))/pow(10,j-1);       //here
    reverse+=x*pow(10,k);
    k++;
    }
 }

 cout<<"Reverse number: "<<reverse;

 getch();
} 

I have no idea why this error is appearing, it would be great if someone could help me with this

oshhh
  • 141
  • 2
  • 10
  • 3
    `#include` I get that you are probably in school right now and have no choice, but as a personal request: Please read some learning material from this decade before you enter the professional world, even anything decent written in the 2000s would be way better. The other two includes and `void main` don't help your learning material's case either, that goes from deprecated to plain illegal. – Baum mit Augen Jan 22 '17 at 05:06
  • 1
    The modulo operator (`%`) can only be used with integral operands. `pow()` returns a `double`, which is not an integral type, even if the value it returns happens to be integral. Try writing your code so it only relies on operations involving integral variables (i.e. exclude the floating point ``). Also, avoid using `` since it is non-standard, and avoid using `` since it is pre-standard (unless you have an ancient compiler, in which case it is better to update). – Peter Jan 22 '17 at 05:07
  • 1
    The issue of using an older compiler is that many answers that are correct will not be availalble with your ancient compiler, or in some cases, implemented differently than the current standard behavior since the compiler is pre-standard. All of this solved by using a modern C++ compiler, not something from 25 years ago. – PaulMcKenzie Jan 22 '17 at 05:22
  • 2
    While @A.S.H has explained your error, your attempted solution is far too complex and convoluted. Challenge: try rewriting to use ***only*** one modulus, one multiplication, one addition and one integer division. (There's absolutely no need for any calls to `pow()`.) – Disillusioned Jan 22 '17 at 05:26
  • @CraigYoung okay,trying that too. But I first need to figure out why this code is wrong...its not giving the right output. The output is coming out to be 0! – oshhh Jan 22 '17 at 05:31
  • 1
    @OsheenSachdev To figure out why it's giving the wrong answer, use a step-by-step debugger. Observe how the values change at each step (compared to how you think they'll change) and you should see where your assumptions are wrong. – Disillusioned Jan 22 '17 at 05:33
  • @OsheenSachdev -- Is this a school assignment? If so, just a warning-- if you handed this in, a good teacher would take points away from you, even if you got this to work. What we're asking you to do is come up with the "classical" solution, without `pow`. In other words, we're doing you a big favor by asking you to rethink this. – PaulMcKenzie Jan 22 '17 at 05:34
  • @PaulMcKenzie thanks for your big favour and I would surely think for a simpler solution, but for now it is more important for me to see where I have gone wrong presently so that I don't make a similar mistake again – oshhh Jan 22 '17 at 05:39
  • The `pow` solution is not a solution, as the [link here](http://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os) demonstrates it. Believe it or not, the solution myself and others are referring two takes 2 lines of code -- no check for even number of digits, no `pow` calls, etc. You will slap yourself silly if we posted that solution and possibly you will quickly erase what work you've done so far. – PaulMcKenzie Jan 22 '17 at 05:42
  • @OsheenSachdev Us giving you the explanation on a silver platter is not going to help you avoid making a similar mistake again (especially since we can't read your mind to pinpoint the mistake in your thinking). However, learning to use the debugger as I've advised ***will help*** you figure out your logic errors in future. (Also, Paul's link in his previous comment is a _powerful_ demonstration of why you should avoid mixing `int` and `double` - that alone will help you avoid many mistakes in future.) – Disillusioned Jan 22 '17 at 07:07

1 Answers1

2

pow is a function that returns a double. and the C++ modulo operator % works only with integer numbers. That's because the mathematical modulo operator is defined for integers. Hence the illegal use.

Besides, you use the check variable without initialization. Initialize all you variables before use, in order to avoid further surprises.

EDIT

Here are some other corrections to do:

check = number before the first for loop.

for(i = 0; check != 0; i++)

Finally, try to find another way to get the digits of the number, without using pow. There are simple ways using consecutive integer division.

A.S.H
  • 29,101
  • 5
  • 23
  • 50
  • Thank you! Made the correction. I have simply changed `pow(10,j)` to `int(pow(10,j))`. But my programming is not giving the desired output. Could you help me with that since you've gone through the code once... – oshhh Jan 22 '17 at 05:21
  • 1
    Why are you using `pow` to reverse a number? The `pow` function should not be used for integer calculations. [See this](http://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os). Your assignment need not use any floating point -- reversing a number doesn't require `pow`, just knowing how to total up integer values in a loop after taking the modulus. – PaulMcKenzie Jan 22 '17 at 05:24
  • 1
    @OsheenSachdev Don't use `pow()` at all. It's unnecessary, adds complexity, introduces possibility of rounding error (made worse when you trunc cast) and is comparatively slower than a simple integers only solution. – Disillusioned Jan 22 '17 at 05:29
  • Oh god what a loser I am! – oshhh Jan 22 '17 at 05:39
  • @OsheenSachdev Don't think like that. Persevere. Everyone struggles at some point. – Disillusioned Jan 22 '17 at 07:00