tags:

views:

108

answers:

1

i have following code

#include <iostream>
using namespace std;

int reverse(int n)
{
    int m=0;
    while (n>0)
    {
        m*=10;
        m+=n%10;
        n/=10;
    }
    return m;
}

int main()
{
    int n=97;
    cout<<reverse(n)<<endl;
    return 0;
}

when i run this code program says access is denied why?

+2  A: 

There are no issues with the code you have posted, so the error may lie in the compiler, OS, or elsewhere in your code if what you have shown is not your entire project (which I doubt).

By the looks of it, you are just doing something horribly wrong.

Oh, and I wouldn't suggest changing the parameters to a function, make a temp value instead:

int reverse(int _n) {
  int n = _n;
  int m = 0;
  while(n>0) {
    m*=10;
    m+=n%10;
    n/=10;
  }
  return m;
}

And you should learn to properly indent your code, for readability sake. Unindented code is as attractive as men in dresses.

Alexander Rafferty
Spoken like a true Scotsman.
anno
`+1` from me for the sentence about indentation. That code hurt my eyes.
sbi
+1 for "indent your code properly," -1 for "make a redundant variable."
avakar
I don't like modifying parameters, it seems improper for some reason.
Alexander Rafferty
It's not modifying the parameter, it's modifying a fresh copy of that parameter, since it's passed by value. Nothing wrong with that, the caller of the function won't notice.
Adhemar