Question

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;
}
Was it helpful?

Solution

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

OTHER TIPS

  1. This: #define MAX_STROKES 1; could become a syntax error wherever you use "MAX_STROKES" (it's your job to figure out "why" ;)).

  2. I hope you have this commented out: GNU nano 2.2.6 File: homework1.c

  3. I'm not sure whether your "printf()" is OK: in your cut/paste, it's cut off here: Ready when yo$

  4. I hope you're #includ'ing all the files you need, like "stdio.h" and "time.h"

Did a little research and decided that the MAX_STROKES macro idea I had wasn't quite right. Thanks guys. My guess is that it wasn't standing for what I wanted. I was looking for "int MAX_STROKES = 1"... Was 1 a char by default as it was? I can't quite tell what exactly it was. After reading I decided to use "static const int MAX_STROKES = 1;" instead and it compiled fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top