You should start by posting your latest code, along with any compiler warnings/errors you're receiving.
You should also post code that is compilable ... that is, don't include line numbers (that is done for you automatically by the forum software).
Make sure your code is neatly formatted and indented. As a courtesy, this is what it should look like:
Code:
#include <stdio.h>
int validCodes(int codes);
int main ()
{
int codes [4] = {303, 720, 719, 970};
int codeToFind = 0;
int codeToCheck = 0;
int i = 0;
int used = 4;
int validCode = 0;
printf("Enter area code to validate: ");
if (scanf("%d", &validCode) == 1)
{
if (validCode(codes));
{
printf("Area code is valid");
}
else
{
printf("Area code is not valid");
}
}
else
{
printf("please try again");
}
return 0;
}
int validCodes(int codes)
{
int validCodes[] = {303, 970, 720, 719};
for(i = 0; i < 4; i++)
ans = codes[i]
return ans;
}
The first mistake is a basic one ... on line 18, you end the "if()" with a semi-colon. That is not what you want.
In C, a semi-colon by itself is considered a valid statement (referred to as a "null statement"). It essentially means "do nothing". That is what you get by placing a semi-colon after the "if".
For instance, in the following code, you would expect the print statement not to print:
Code:
int x = 5;
if(x > 10);
printf("x is %d\n",x);
But it does print. Because of the misplaced semi-colon, the code is really interpreted as this:
Code:
int x = 5;
if(x > 10)
;
printf("x is %d\n",x);
Regardless of the value of "x", the print statement is executed. If "x" is not greater than 10, the "if" fails and the rest of the code (including the print) occurs. If "x" is greater than 10, then the "if" is true ... but the null statement says nothing is to occur ... and the rest of the code (i.e. the print) still happens.
Your code has still other errors.
You're using the variables "i" and "ans" in the "validCodes()" function, but you don't declare those variables in that function.
If you pass an array to a function, you need to declare it as an array (or a pointer) in the function parameter list.
And in your "validCodes()" function, you're not using the "validCodes" array for anything.