Thread: Changed Program, Now Getting Syntax Errors

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    38

    Changed Program, Now Getting Syntax Errors

    Here's my program as it stands now. It's supposed to read in values from a file, which is supplied on the command line. If the values start with an alpha, they are ignored. Otherwise, they are supposed to be converted to floats, and then averaged.

    Here's the code, currently giving me syntax errors on the bold line:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char *argv[]){
        FILE* dataFile;
        FILE* outputFile; 
        char stringArray[1000][80];
        float floatArray[1000];
        int i = 0;
        int j = 0;
        int number_of_lines;
        int number_of_floats;
        float sum_of_floats;
        float average;
        
        dataFile = fopen(argv[1], "rb");
        if (dataFile == NULL){
           fputs("File error", stderr);
           exit(1);
           }
    
        while (1){
              fscanf(dataFile, "%s", stringArray[i]);
              if (stringArray[i][0] == EOF){
                 break;
                 }
              i++;
              }          
        fclose(dataFile);
        number_of_lines = (i+1);
        
        for (i=0; i<number_of_lines; i++){
            if (isalpha(stringArray[i][0])){
               i++;
               }
            else{
                 floatArray[j] = strtof(&stringArray[i],&&stringArray[i][79]);
                 j++;
                 }
            }
        number_of_floats = (j+1);
        
        for (i=0; i<number_of_floats; i++){
            sum_of_floats += floatArray[i];
            }
        
        average = ((sum_of_floats)/((float)(number_of_floats));
        
        printf("%f", average);
        
        fclose(dataFile);
        getch();
        return 0;
    }
    The syntax errors are:
    Code:
    51 C:\Documents and Settings\Don\My Documents\Online Assignments\autocorrelation.c syntax error before '[' token 
    61 C:\Documents and Settings\Don\My Documents\Online Assignments\autocorrelation.c syntax error before ';' token 
    51 C:\Documents and Settings\Don\My Documents\Online Assignments\autocorrelation.c label `stringArray' used but not defined
    And that's before I even access the file, which is of the form:
    Code:
    C HERE'S A COMMENT
    C HERE'S ANOTHER COMMENT
    234.5
    -123.67
    0.000
    10.10
    .
    .
    .
    Am I just doing something completely stupid or what?

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    You have double &-signs. You're trying to get the address of an address..
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And to clarify, strtof()'s second parameter is supposed to be the address of a pointer that you wish to fill in with the pointer to where in the string the input was stopped (e.g. if you have "123.45 234.56", the pointer would point at the space in the middle. You can use this for two things:
    1. Determine if the entire string was accepted (the pointer will then point to a NUL character).
    2. If you have multiple floats in a single string, the endPtr will indicate where the last field ended, so you can easily move on to the next field (in the case of space, it will be ignored in the beginning of the number, so it would just be case of passing the endPtr as input to strtof() to read the 234.56 next).

    It can also be NULL, if you don't care about where the input ended.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    Each float occupies its own line, so what value can I assign to the second pointer so that it just moves to the next line naturally?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DonFord81 View Post
    Each float occupies its own line, so what value can I assign to the second pointer so that it just moves to the next line naturally?
    Well, assuming you use fgets() or some such that reads a line at a time, then you can't use that method. If that's the case, and you do not wish to check that the input was fully processes, I suggest you pass NULL as the second argument. And use a loop like you do to work your way through the data.

    Code:
              fscanf(dataFile, "%s", stringArray[i]);
              if (stringArray[i][0] == EOF){
                 break;
                 }
    This is wrong - stringArray[i][0] does not get set to EOF (it can not be, since it's a char, and EOF is an integer value). The return value from fscanf() will be set to EOF if the file ended (or some other error prevents reading the data).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Further to the previous post: I don't really see the point in reading each line into a big array, and then translating each entry to a float. Why not just read one line, and translate it to float then and there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    I need to make sure each line is, in fact, a float before I convert it, don't I?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DonFord81 View Post
    I need to make sure each line is, in fact, a float before I convert it, don't I?
    Yes, you would still need to check if it is a floating point number or not - but you don't need to read ALL lines FIRST, then convert them later on - just check the string, then convert it, as part of the reading process - there is no need to have an array 1000 strings stored, when you only need one string at a time to convert the content - unless I've missed something in what your program does.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    So, something more along the lines of this:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char *argv[]){
        FILE* dataFile;
        FILE* outputFile; 
        char string[80];
        float floatArray[1000];
        int i = 0;
        int number_of_lines;
        int number_of_floats;
        float sum_of_floats;
        float average;
        
        dataFile = fopen(argv[1], "rb");
        if (dataFile == NULL){
           fputs("File error", stderr);
           exit(1);
           }
    
        while (1){
              fgets(string, 80, dataFile);
              if (!isalpha(string[0])){
                 floatArray[i] = strtof(&string[0], NULL);
                 i++;
                 }
              }          
        fclose(dataFile);
        number_of_floats = (i+1);
    
        for (i=0; i<number_of_floats; i++){
            sum_of_floats += floatArray[i];
            }
        
        average = ((sum_of_floats)/((float)(number_of_floats));
        
        printf("%f", average);
        
        fclose(dataFile);
        getch();
        return 0;
    }
    ?

    ...

    And now it's giving me a system error and shutting down prematurely.
    Last edited by DonFord81; 03-05-2009 at 09:57 AM. Reason: New Problem

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Probably not your main problem, but there's no guarantee that sum_of_floats is meaningful when your program starts. You also probably don't want to open your file in binary mode, if you're going to use fgets on it.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    &string[0]
    is the same as
    Code:
    string
    in this case.

    Put the fgets() in the while-loop so that you end the loop when you hit the end of the file - I suspect your program crashes because you are using more than 1000 entries of the float array, becuase it keeps repeating the last actual line of the file, and crashing when it runs over the end of the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    Quote Originally Posted by matsp View Post
    Put the fgets() in the while-loop so that you end the loop when you hit the end of the file - I suspect your program crashes because you are using more than 1000 entries of the float array, becuase it keeps repeating the last actual line of the file, and crashing when it runs over the end of the array.

    --
    Mats
    That did it! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. 3x syntax errors :S, Help a student finalise an assignment
    By Chickenhawk in forum C Programming
    Replies: 14
    Last Post: 07-27-2006, 05:14 AM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM