# Thread: this code won't work

1. ## this code won't work

good morning ;
this programme is created to find x^y
but it show every time an random number !!!
and thank you so much ....
Code:
```#include <stdio.h>
int nbpos (int x)
{
int c,cpt ;
do
{
x=x/10;
c++;
}
while (x!=0);

return c;
}

int puiss (int x , int y)
{
int i=1,s=1;
printf ("\n--- x == %d --- s == %d---- y == %d\n",x,s,y);
/*
for (i=1;i<y;i++)
{
s=s+1;

}
*/
return s;
}

main()
{
int x=10,y,e;

scanf("%d",&y);
printf (" %d  ********** \n",nbpos(y));

e=nbpos(y);
e=puiss (x,e);
printf("////// %d \n",e);
}```

2. Originally Posted by med linux
but it show every time an random number !!!
Code:
```#include <stdio.h>
int nbpos (int x)
{
int c,cpt ;
do
{
x=x/10;
c++;
}
while (x!=0);

return c;
}```
Notice that c is not initialized to any particular value, so it's value is random.

3. Originally Posted by med linux
good morning ;
this programme is created to find x^y
but it show every time an random number !!!
and thank you so much ....
Please don't use giant colored text... It makes you look like an idiot.

Code:
```#include <stdio.h>
int nbpos (int x)
{
int c,cpt ;
do
{
x=x/10;
c++;
}
while (x!=0);

return c;
}```
your integer c is used without initialization... so you're doing math on garbage.
cpt is never used.

4. I'm assuming that x^y means x to the power of y? If so, this isn't even close.

The function nbpos is returning the exponent part of the number when expressed in scientific notation, so 7 = 7E0 --> nbpos(7) = 0, and 286=2.86E2 --> nbpos(286) = 2.

Currently, the puiss function is always returning 1. With the comments removed, puiss is returning y+1, and x isn't used. The y you're passing in is the return value of nbpos (e in main).

What you should be doing to calculate x^y is going through a loop to multiply x by itself y times.

EDIT: I didn't catch that c wasn't being initialized in nbpos. Still, even if it was, the above still stands.

5. Originally Posted by CommonTater
Please don't use giant colored text... It makes you look like an idiot.

your integer c is used without initialization... so you're doing math on garbage.
cpt is never used.
the problem is in the color or in the color and the size ?

6. the problem is in the color or in the color and the size ?
Yes

Jim

7. Originally Posted by med linux
the problem is in the color or in the color and the size ?
Both...

When I see giant red text the first thing that goes through my mind is "10 year old at the keyboard".

In fact, it almost certainly has the opposite of the effect you intended...
When I see crap like that I generally just go read something else.

8. Fixed colour and size