the program just compiles ok but it just doesn't work i don't know where is the fault.Can someone help me out???

output looks like this:

Enter the hex value:

hex value must be preceded by 0x or 0X

0x03f

error:invalid hex value

/*htoi() converts char string to a decimal value */ #include<stdio.h> #include<stdlib.h> int ctoi(int,int); int main() { int i = 0,p = 0,dec = 0,res = 0; int s[8]; printf("Enter the hex value:\nhex value must be preceded by 0x or 0X\n"); while((s[i]=getchar())!='\n'){ i++; } if(s[0] =='0') { if((s[1]=='x')||(s[1]=='X')) { while(i>=2) { res = ctoi(s[i],p); dec = dec + res; p++; i--; } printf("decimal value of hex number is : %d\n",dec); } else{printf("hex value must begin with 0x or 0X\n");getchar();exit(0);} } else{printf("hex value must begin with 0x or 0X\n");getchar();exit(0);} getchar(); return 0; } int ctoi(int c ,int p) { int n = 0; if(c >='A'&&c<='F') { n = c - 55;while(p>=1){n = n*16;p--;} } else if (c>='a'&&c<='f') {n = c-85;while(p>=1){n = n*16;p--;}} else if(c>='0'&&c<='9') {n=c-48;while(p>=1){n = n*16;p--;}} else{printf("error:invalid hex value");exit(0);} return n; }