cgettimeofdaytimeval

Strange errors using timeval struct and gettimeofday —because of semicolon in #define


I am getting a couple of weird compilation errors. This is for a homework assignment (help is ok). The idea is to implement a program that tests how well the user can hit "enter" once a second. I'm supposed to use gettimeofday to get some time values for each "enter" and then find out what the average time is and standard deviation... I'm trying to do this by checking stdin for '\n' and then if true, using gettimeofday to populate a timeval struct, then store said struct in an array for later use...

The errors I'm getting when compiling (gcc -Wextra homework1.c) are:

homework1.c: In function ‘main’:
homework1.c:19:29: error: expected ‘]’ before ‘;’ token
homework1.c:27:17: error: expected ‘)’ before ‘;’ token
homework1.c:32:4: error: ‘entry_array’ undeclared (first use in this function)
homework1.c:32:4: note: each undeclared identifier is reported only once for each function it appears in

I can't see why I'm getting those first two syntax errors and then I can't understand why "entry_array" is undeclared when I am clearly declaring it in the beginning of "main." Suggestions?

I feel like I'm getting burned by not knowing how to use the timeval struct... Initially I tried to define the struct timeval globally like you would with any other struct but was getting an error about overwriting the definition for struct timeval... Is this because it is defined in the "sys/time.h" library?

Here's the code:

  GNU nano 2.2.6                        File: homework1.c                                                       
//prototypes 
int GetAverage(long array[]);
int GetStdDev(long array[]);

//# of keystrokes tracked by user
#define MAX_STROKES 1;



int main(int argv, char ** argc) {
        struct timeval entry_array[MAX_STROKES]; //container for tv_usec fields from timeval struct
        double average = 0; 
        double std_deviation = 0;
        int count = 0; 

        printf("This program will test your ability to hit enter every 1 second, for 10 seconds.  Ready when yo$

        //loop to build array of timeval's
        while (count < MAX_STROKES) {
                struct timeval time_val;
                int input = getc(stdin);
                if (input == '\n') {
                        gettimeofday(&time_val, NULL);
                        entry_array[count] = time_val;
                        ++count;
                }
        }
        return 0;
}

Solution

  • The problem is the MAX_STROKES macro. As this is homework, I won't tell you exactly what the problem with it is.