# Thread: need help fixing excercise program...

1. ## need help fixing excercise program...

this is based on a problem from an exercise book i found...

in essence you're supposed to write a program that receives an integer (not necessarily positive), and perform the following check on it:

every digit in an odd place should be odd and vise versa.
so for example:
4723 is fine
3301 is not
digits are counted right to left and there's no restriction to the size of the input.

okay so here's what i got:

Code:
```#include <stdio.h>

int run=1, input, fixed, i=1, mod, check=1;
int sign_fix(int input);

main()
{
while(run>0)
{
printf("input an integer:\n");
scanf("%d",&input);

fixed=sign_fix(input);

while(fixed>=0,check=1)
{
if((fixed%2)==i)  // checks whether %2 is even or odd. i changes after every successful incident
{
i=(i+1)%2;

fixed=fixed/10;
}
else
check=0;
}
if(check>0)
printf("YES \n");    //conforms to the requirements
else
printf("NO \n");     // duh
}
}

int sign_fix(int input)     // checks whether input is positive or negative and returns accordingly
{
if(input>=0)
return input;
else
return -input;
}```
the code compiles fine, but the program freezes somewhere after the scanf

anyone know what the deal is ?
and, in general, how does one go about solving such problems ?
any tricks ?

2. Code:
`		while(fixed>=0,check=1)`
This is certainly not doing what you think it does, since it's pointless to do fixced >= 0 when you are doing while(1) which is what the second part does.

The comma operator is probably the most commonly misunderstood part of the C language.

What you probably meant is to use && to check if fixed is greater or equal to zero, AND that check is equal to 1 - which also means that you need to use "is equal"(==) rather than "assign"(=).

--
Mats

3. fixed the && and the ==

lost the fixed >=0 completely...

i also left out a contingency...
the difference between receiving 0 and fix becoming 0...

after i fixed that it worked fine...

thanks mat(s/e)... (how terribly droll )