My code isnt working (SOLVED)

NOW MY CODE IS WORKING. THE PROBLEM WAS: IN C++ pow(10,2) = 99.99999.... and if you say int k= pow(10,2) , then k = 99... so actually, if you want to get 100,1000,10000, you need to write
k = pow(10,x) + 0.1, x could be any positive integer...then, result will be
k = 100.09999 (for exponenet= 2) but since k is int, then k = 100
ANOTHER SOLUTION is to write pow(10.0,2) instead of pow(10,2), that works perfectly!

My code

#include <iostream>
#include <math.h>
using namespace std;

int invbr =0;
int Inv(int u)
{
int f;
int p =1;
int j=1;
int brojac =0;
int y =u;

while(y>0)
{

y /=10;
brojac = brojac + 1;
}
cout << brojac << endl;
p=brojac -1;
for( p; p>=0; p-=1)
{


f = pow(10,p);
int q = u/j;


invbr += (q%10) * f;
j*= 10;
}

cout <<invbr <<endl;


}


int main()
{
int A;
cin >> A;
Inv(A);

return 0;
}
Last edited on
@Optx

You need to initialize invbr at the top of your function as int invbr = 0;, otherwise the program doesn't know what it is. Also, you can make the function a void instead of an int, since you aren't returning anything. After I did those things, the program ran as expected.
1) Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
2) Just a suggestion: I would have your Inv() function return invbr, then cout it in main(). That way, if you wanted to do something later like doubling the result of the digit inversion, then you could just do std::cout << (Inv(a) * 2) << std::endl;
whitenite1 thanks for reply! But, I have already put inbr= 0 on the top, but I did not post whole code because i have other functions ..I edited code so it actually looks like this.
@Homberto
2) I know, first I tried that way, but I tried like this because I thought maybe there will be no errors.But same again
Topic archived. No new replies allowed.