Thread: Help, getting unknown error

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    4

    Help, getting unknown error

    Hi,

    I keep getting this error

    cw_testca.c:52: error: expected ‘)’ before ‘[’ token

    I have no idea what is the issue. I guess it's something to do with my function.

    Code:
    #include <stdio.h>
    
     int i, j, num;
     float x[10000], y[10000], xsum, ysum, xbar, ybar, xmax, ymax, xmin, ymin;
    
    
     
     
    main()
    {
      FILE *inputfile;
      char filename[33];
      char text[11];
     
      
    
      printf("Enter name of datafile: ");
      scanf("%s",&filename);
      if ( (inputfile = fopen(filename, "r")) == NULL)
    {
        printf("Error opening file\n"); 
    }
    
    else {
        printf("File opened successfully.\n");
    
    i = 0 ;    
    
        while(!feof(inputfile)) { 
          /* loop through and store the numbers into the array */
          fscanf(inputfile,"%f %f", &x[i], &y[i]);
          i++;
        }
    
        printf("Number of numbers read: %d\n\n", i);
        printf("The numbers are:\n");
    
        for(j=0 ; j<i ; j++) { /* now print them out 1 by 1 */
         printf("%f %f\n" , x[j], y[j]);
        }
    
    minimum_xvalue(x[j]); /*determine and print minimum x value*/
    
    
      fclose(inputfile);
      return 0;
    }
    }
    
    
    
    minimum_xvalue(x[j])
    
      { xmin = x[0];
    
    for (j=1; j<num; j++)
    {
      if (x[j] < xmin)
          xmin = x[j];
      printf("Minimum value of x is %f\n" , xmin);
      return 0;
    }
    }
    My input file is a random set of x, y pairs:
    3.456 1.001
    2.394 2.398
    2.345 2.3456
    8.345 9.345
    3.56 10.2344
    5.234 0.002
    4.209 3.590
    1.2034 2.344
    3.455 0.928
    1.345 9.123
    0.212 0.23

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This:
    Code:
    minimum_xvalue(x[j])
    is not a valid function definition. You could use:
    Code:
    int minimum_xvalue()
    since x is global. In which case you also have to change the call (right now, the call and the definition are identical). Or you could change the definition:
    Code:
    int minimum_xvalue(float x[1000])
    Either way, your next error (which will show up when you fix this one) will have to do with minimum_xvalue() being after main with no prototype declared. You need to add a prototype for the function, at the top.
    Last edited by MK27; 01-28-2010 at 06:30 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Also, both "main" and "minimum_xvalue" are returning a value ("return 0" in both cases), however you never stated what the functions' return type even are. Sure, there may be some "default"s or something that your compiler is assuming, but continuing that way certainly isnt good practice.

    "main" should have return type "int". So "main()" becomes "int main()", or one of the other standard ways in that link.

    Also, your "minimum_xvalue" function always returns the same value. If it doesnt make sense for the function to return anything, then declare it as "void minimum_xvalue()".

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nadroj View Post
    Also, both "main" and "minimum_xvalue" are returning a value ("return 0" in both cases), however you never stated what the functions' return type even is.
    And egads, I didn't include that. Will correct my post...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    4
    Hi thanks for the reply.
    I think I am not understanding you.
    I changed the function definition to minimum_xvalue() and left the call as is. The code is running, but I am not getting no output from the function. I tried playing around with the definition and the call with both suggestions but nope.
    With a prototype set before main I am getting the error
    cw_testca.c:7: warning: data definition has no type or storage class

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Post the code you are using now.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    4
    Thanks.

    Code:
    #include <stdio.h>
    
     int i, j, num;
     float x[10000], y[10000], xsum, ysum, xbar, ybar, xmax, ymax, xmin, ymin;
    
    minimum_xvalue();
     
    int main()
    {
      FILE *inputfile;
      char filename[33];
      char text[11];
     
      
    
      printf("Enter name of datafile: ");
      scanf("%s",&filename);
      if ( (inputfile = fopen(filename, "r")) == NULL)
    {
        printf("Error opening file\n"); 
    }
    
    else {
        printf("File opened successfully.\n");
    
    i = 0 ;    
    
        while(!feof(inputfile)) { 
          /* loop through and store the numbers into the array */
          fscanf(inputfile,"%f %f", &x[i], &y[i]);
          i++;
        }
    
        printf("Number of numbers read: %d\n\n", i);
        printf("The numbers are:\n");
    
        for(j=0 ; j<i ; j++) { /* now print them out 1 by 1 */
         printf("%f %f\n" , x[j], y[j]);
        }
    
    minimum_xvalue(x[j]);
    
    
      fclose(inputfile);
     return;
    }
    }
    
    
    int minimum_xvalue()
    
      { float xmin = x[0];
    
    for (j=1; j<num; j++)
    {
      if (x[j] < xmin)
          xmin = x[j];
      
      printf("Minimum value of x is %f\n" , xmin);
      return xmin;
    }
    }

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Here is a simple example to show a function prototype. The logic of the example is irrelevant to your code, as it is just an example of declaring a function prototype.
    Code:
    // include headers, etc.
    
    // function prototype: what this tells the compiler is that it returns "int" and takes exactly 2 arguments, both of "int" type
    int addTwoNumbers(int,int);
    
    int main(void)
    {
       int a = 1;
       int b = 2;
       
       int c = addTwoNumbers(a, b);
       // 'c' was set the return value of the function, which was 1+2=3
      
       return 0;
    }
    
    // function definition
    int addTwoNumbers(int x, int y)
    {
       return x + y;
    }

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    You still have the initial problem of not stating what the return type of the function is. The prototype at the top "minimum_xvalue();" should be "void minimum_xvalue();" or "int minimum_xvalue();", or whatever return type you want it to be.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Some further dissection:

    type name(parameters);
    Code:
    int minimum_xvalue(float x[1000]);
    You need all three, altho the last one can be just ().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    int minimum_xvalue()
    {
      float xmin = x[0];
    
      for (j=1; j<num; j++)
      {
        if (x[j] < xmin)
          xmin = x[j];
      
        printf("Minimum value of x is %f\n" , xmin);
        return xmin;
      }
    }
    From this, you can see that after the first "loop" (or iteration) of the "for" loop the function returns ("finishes"), so that the loop never goes through a second iteration, or third, etc. Basically, this function is equivalent to this:
    Code:
    int minimum_xvalue()
    {
      float xmin = x[0];
      
      j = 1;
      if ( j < num)
      {  
        if (x[j] < xmin)
          xmin = x[j];
    
        printf("Minimum value of x is %f\n" , xmin);
        return xmin;
      }
    }
    Is this what you really want?

    Also,
    The code is running, but I am not getting no output from the function.
    This is pretty ambiguous and we dont know what you mean by this. We can assume or guess what we think you mean, but that is useless and a waste of time. Be precise and tell us the exact problem. We could fix it completely by taking our time to read and debug every line, but I, for one, will not do that. However, I will help if you help us help you.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I posted at the same time as you, see my previous post.

    Quote Originally Posted by halien
    The code ran, but my function isn't giving me any output. I changed float to int ... and still nothing.
    I suggest not randomly changing things and expect it to "work". You should try to understand what you are doing, and why you are doing it.

    If you are taking a "programming course" as an elective or optional course, then you might be able to get by (i.e. D-) continuing this way. However, if this is your major, you should take your time as this is an extremely fundamental concept.

    If you are offended at all by this, well, try not to be, because that is not my intention.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM