i trying write program validates area code using c. user enters area code , program checks if valid. doing array. when run program , enter valid area code return "invalid area code." think there problem logic.
also when compiling warning: comparison between pointer , integer
.
how fix this?
here code:
#include <stdio.h> int main() { int codetocheck[4] = {303, 720, 970, 719}; int codetofind = 0; int = 0; printf("enter area code validate: "); scanf("%d", &codetofind); for(i = 1; <= 4; i++) { codetocheck[i] = codetofind; if(codetofind == codetocheck) break; } if(codetofind == codetocheck) printf("\nvalid area code in colorado!"); else printf("\ninvalid area code in colorado"); return 0; }
the problem if(codetofind == codetocheck)
statement, here checking value of integer base address of array.
if(codetofind == codetocheck) //correct comparison of array elements codetofind == codetocheck[i]
also core logic not correct, in code posted condition inside if
true because in statement above if
assigning codetocheck[i]
codetofind
value, , in next line comparing same values!!
you can correct code correct output
int flag = 0; for(i = 0; <= 4; i++) //you should correct line, array indices starts 0 { if(codetofind[i] == codetocheck) { flag = 1; break; } } if(flag) printf("\nvalid area code in colorado!"); else printf("\ninvalid area code in colorado");
Comments
Post a Comment