I'm writing a program that reads in 30 numbers from a file stores them in an array then passes that array to a function which will print out only the even numbers from that array. However, I'm getting a Bad Access Code error in my while statement.
Any suggestions? Thanks in advance!
#include <stdio.h>
void even(int numbers[]);
int main(int argc, const char * argv[]) {
FILE *file = fopen("input.txt", "r");
if (file == NULL) {
printf("error");
}
int numbers[30];
int num = 0;
int i = 1;
while(fscanf(file, "%d,", &num) > 0) {
numbers[i] = num;
i++;
}
fclose(file);
even(numbers);
return 0;
}
void even(int numbers[]){
for (int i = 0; i < 30; i++) {
if (numbers[i] %2 == 0) {
printf("%d", numbers[i]);
}
}
}
while (fscanf(file, "%d, ", &num) > 0)is strictly wrong. Please readscanf(3).printf("error");-->printf("error");return -1;