Thread: Changed Program, Now Getting Syntax Errors

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.

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