I'm new to C Programming ( 6th week of an 8 week course ) and I am working on user defined structures. I have created a structure to store data received from a file and three of the four sub functions planned: open the file, store the data in the file (with error control measures), and determine the maximum variation in temperature. In the code I am posting, I have not created the MAIN function yet. I am strictly working on the sub-functions at this time. I am getting the error "expected identifier before ',' token on lines 19 and 83. Can someone show me (explain to me in layman's terms) my error in this function prototype and call? The highlighted function is supposed to pull data from the structure, create a local array to store calculated information, compare the "id" field in the structure (for control purposes) and then calculate the maximum variation for temperatures associated with that ID. If the ID does not match, then the current minimum value of temperatures is subtracted from the maximum value of temperatures stored in the correct array position along with the ID. Once the entire structure is gone through, the values of the ID and maximum variance are to be returned to the main function (which isn't created yet).

/*
 * Program pulls data from a text file, determines maximum temperature variation and
 * highest average wind speed between several locations using user created structured
 * data types.
 */

 #include <stdio.h>

 typedef struct
 {
 int id;         // 4 digit ID is used to track location for calculations
 int day;        // 2 digit numerical day of month
 int ws;         // ws is the wind speed in knots
 int temperature;// daily temperature measurement
 } measured_data_t;

 int fscan_data(FILE *filep, measured_data_t *unitp);
 void load_data(int data_max, measured_data_t data[], int *data_sizep);
 int max_temp(measured_data_t id, measured_data_t temperature, , int *ID, int *max_temp);
 // insert declaration of function to get max temp variation
 // insert declaration of function to get highest average windspeed

/*
 * Gets data from a file to fill output arguments
 * Returns standard error code: 1=> successful input, 0 => error, negative EOF value => EOF
 */
 int
 fscan_data(FILE *filep, measured_data_t *unitp)
 {
     int status;

     status = fscanf(filep, "%d%d%d%d", unitp->id,
                                        unitp->day,
                                        unitp->ws,
                                        unitp->temperature);
    if (status == 4)
        status = 1;
    else if (status != EOF)
        status = 0;

    return (status);
 }

 /* Opens file weather_data.txt and gets data to place in data until EOF is found.
  * stops early if there are more than unit_max data or invalid data.
  */
  void
  load_data( int data_max, measured_data_t data[], int *data_sizep)
  {
      FILE *inp;
      measured_data_t data_in;
      int i, status;

      // Gets info from file
      inp = fopen("weather_data.txt", "r");
      i = 0;

      for (status = fscan_data(inp, &data_in);
           status == 1 && i < data_max;
           status = fscan_data(inp, &data_in))
        {
            data[i++] = data_in;
        }
        fclose(inp);

        // Error message for early exit
        if (status == 0)
        {
            printf("\n***** Error in data format in txt file *****\n");
            printf("***** Using the first %d data values *****\n", i);
        }
        else if (status != EOF)
        {
            printf("\n***** Error file contains too many sites *****\n");
            printf("\n***** Using first %d data values *****\n", i);
        }
        // Return the size of the used portion of the array
        *data_sizep = i;
  }

  /* Calculates max temperature variation for sites and returns the ID and max_temp to main */
 int
 max_temp(measured_data_t id, measured_data_t temperature, , int *ID, int *max_temp);
  {
      int ID[], MAX_VAR[]; // Array to store the Maximum variation in temperature with its ID
      int max_temp = 0;    // Set to zero so it will increase
      int min_temp = 100;  // Temp is air temp in degrees C, so set to above max expected
      int big_val;

      if(measured_data_t.id == ++measured_data_t.id)
        {
            if (measured_data_t.temperature < min_temp)
                min_temp = measured_data_t.temperature;
            if (measured_data_t.temperature > max_temp)
                max_temp = measured_data_t.temperature;
        }

      else if(measured_data_t.id != EOF)
        {
            max_tem_var = (max_temp - min_temp);
            ID[i] = measured_data_t.id;
            MAX_T[i] = (max_temp_var);
            ++i;
        }
      else if(measured_data_t.id == EOF)
      {
            for(i=0; i<sizeof(MAX_T); i++;)
                {
                    if (big_val < MAX_T[i])
                       {
                            big_val = MAX_T[i]
                            return(ID[i], max_temp[i]);
                       }
                }
      }
  }

// insert main

I may be seriously over thinking the third structure, but once I get the function to work, create the fourth function, and the main, I will work on my logic errors. Right now I am focused simply on the error regarding the "expected identifier". I am going to start work on the fourth function while waiting on advice, but I assume that I will probably have the same error in calling that function since it works with the same data except it calls "ws" (wind speed) from the structure instead of temperature.

Edited 6 Years Ago by Griff0527: n/a

int max_temp(measured_data_t id, measured_data_t temperature, , int *ID, int *max_temp);

You have two , , with no parameter. Probably should be

int max_temp(measured_data_t id, measured_data_t temperature, int *ID, int *max_temp);

Edited 6 Years Ago by gerard4143: n/a

Wow... OK, now I feel like I need to get my eyes checked out. Thanks for the catch gerard4143. I also had a ';" on line 83 after calling the function.. That's what copy and paste will get me. I'm going to leave this thread open as I'm sure I will have more issues with this code before it is completed. In fact, a whole slew of other errors already came up, but I'm going to work on them for a while and see what I can learn before asking for more help. One question though, If you look at the if statement on line 89, is that comparison legal? I get the same sort of error about not having a "primary-expression" and I am new to user-defined structures, so I am not sure I am doing that portion "legally".

When your new to C programming you should think of programing as set of steps and smaller the steps the better.

Program a step, compile and fix all errors and warnings. If possible try running the program. Does it produce correct results? If not - correct and recompile.

As a novice you should never get into a situation where you have page(s) of code which has not been compiled and run...Actually this advice applies to all programmers not just novices

As for your if statement...No I won't make that comparison.

Edited 6 Years Ago by gerard4143: n/a

OK. Thank you. I'll go ahead and write a main() function to produce some sort of output for debugging purposes. In the first two functions I wrote, I did write, save, compile, and debug each one before moving to the next one. The third function is where I ran into the error I could not see (though it was a simple error of the extra ',' and the ';' at the end of the function. Currently, I am sorting out some redundant variables and the variable I named that shadows the parameter in the call (more aggressive attention to detail needed on my part). I appreciate the advice so far with this. Do you have a recommendation or advice on how you would approach the comparison in line 89? I don't mean this insultingly or harshly, but your answer of "No, I would not make that comparison" was a little ambiguous, but then again, it WAS a direct answer to my question as well.

I am trying to not post the code redundantly as I appreciate the back and forth conversation that lets me figure the issues out on my own. However, an example of how you would perform the comparison in line 89, or a pseudo-code variation would be most appreciated.

OK. Thank you. I'll go ahead and write a main() function to produce some sort of output for debugging purposes. In the first two functions I wrote, I did write, save, compile, and debug each one before moving to the next one. The third function is where I ran into the error I could not see (though it was a simple error of the extra ',' and the ';' at the end of the function. Currently, I am sorting out some redundant variables and the variable I named that shadows the parameter in the call (more aggressive attention to detail needed on my part). I appreciate the advice so far with this. Do you have a recommendation or advice on how you would approach the comparison in line 89? I don't mean this insultingly or harshly, but your answer of "No, I would not make that comparison" was a little ambiguous, but then again, it WAS a direct answer to my question as well.

I am trying to not post the code redundantly as I appreciate the back and forth conversation that lets me figure the issues out on my own. However, an example of how you would perform the comparison in line 89, or a pseudo-code variation would be most appreciated.

When a programmer checks for EOF, its usually done as part of the read/get...

#include <stdio.h>

int main(int argc, char**argv)
{
	unsigned int ch;

	while ((ch = getc(stdin)) != EOF)/*part of the read/get*/
	{
		fputc(ch, stdout);
	}
	return 0;
}

Edited 6 Years Ago by gerard4143: n/a

I figured out the proper code for this function and am posting it here as a reference in case anyone else is looking for an answer along these lines.

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 1000
#define MAX_SITE 10

typedef struct
{
  int id;         // 4 digit ID is used to track location for calculations
  int day;        // 2 digit numerical day of month
  int ws;         // ws is the wind speed in knots
  int temperature;// daily temperature measurement
} measured_data_t;

int fscan_data(FILE *filep, measured_data_t *unitp);
void load_data(int data_max, measured_data_t data[], int *data_sizep);
int max_temp(measured_data_t id, measured_data_t temperature, int *ID, int *max_temp);
void solve(measured_data_t data[], int size);


/* Gets data from a file to fill output arguments*/
 int
 fscan_data(FILE *filep, measured_data_t *unitp)
 {
     return fscanf(filep, "%d%d%d%d", &unitp->id,
                                      &unitp->day,
                                      &unitp->ws,
                                      &unitp->temperature);
 }

/* Opens file weather_data.txt and gets data to place in data until EOF is found.*/
void
load_data(int data_max, measured_data_t data[], int *data_sizep)
{
  FILE *inp;
  measured_data_t data_in;
  int i, status;

  // Gets info from file
  inp = fopen("weather_data.txt", "r");
  if (!inp)
   {
    printf("\nCannot open input file!\n\n");
    exit(-1);  // Standard error output from <stdlib.h>
   }

  for (i = 0; i < data_max && (status = fscan_data(inp, &data_in)); )
    data[i++] = data_in; // Build data array from input scan
  fclose(inp);

  // Return the size of the used portion of the array
  *data_sizep = i;
}

/* Build array of IDs */
int findSiteId(int ids[], int id, int idsAmount) {
  int i;
  for (i = 0; i < idsAmount; ++i)
    if (ids[i] == id)
      return i;
/* If error, return -1 and exit */
    return -1;
}

/* Perorm all calculations */
void solve(measured_data_t data[], int size) {
  int ids[MAX_SITE], idsAmount = 0;
  int minTemp[MAX_SITE], maxTemp[MAX_SITE];
  double avgSpeed[MAX_SITE] = {0};
  int recordsAmount[MAX_SIZE] = {0};
  int i, site1, site2;

  for (i = 0; i < size; ++i) {
    int index = findSiteId(ids, data[i].id, idsAmount);
    if (index == -1) {
      index = idsAmount++;
      ids[index] = data[i].id;
      minTemp[index] = maxTemp[index] = data[i].temperature;
    } else {
      if (minTemp[index] > data[i].temperature)
        minTemp[index] = data[i].temperature;
      if (maxTemp[index] < data[i].temperature)
        maxTemp[index] = data[i].temperature;
    }

    ++recordsAmount[index];
    avgSpeed[index] += data[i].ws;
  }

  site1 = 0;
  site2 = 0;
  for (i = 0; i < idsAmount; ++i) {
    // calculate and check for greatest average wind's speed
    avgSpeed[i] /= recordsAmount[i];
    if (avgSpeed[site2] < avgSpeed[i])
      site2 = i;
    // check temperature variation
    if (maxTemp[i] - minTemp[i] > maxTemp[site1] - minTemp[site1])
      site1 = i;
  }

  printf("\n\nSite with greatest variance in temperature is %d,\n         with a variance of %d degrees Celcius.\n\n\n", ids[site1], maxTemp[site1] - minTemp[site1]);
  printf("Site with greatest average wind's speed is %d,\n         with an average speed of = %.2lf knots.\n\n", ids[site2], avgSpeed[site2]);
}

int main(void)
{
  measured_data_t data[MAX_SIZE];
  int size = 0;

  printf("This program recieves input from a file named 'weather_data.txt' and\n");
  printf("calculates the greatest variation in temperature as well as the highest\n");
  printf("average wind speed for all the days in the file (up to a maximum of\n");
  printf("ten sites may be stored in the data file.  This program will then print\n");
  printf("the site ID of the site with the greatest variation in temperature on one line\n");
  printf("along with the value of that temperature, and the site ID of the site with the\n");
  printf("highest average wind speed along with the wind speed.\n\n");
  printf("Press any key to run this program and pull data from the file.\n\n\n");

  system("pause");

  load_data(MAX_SIZE, data, &size);

  solve(data, size);

  return 0;
}
This question has already been answered. Start a new discussion instead.