简体   繁体   中英

Why doesn't my function store the values I want it to when reading data from a file?

I'm writing a function which reads data from a file containing 15 series. Each series ends with the value 0.0. series[].totnr counts the number of values in each series, series[].outnr counts the number of values that is 5% above or below 220, and series[].average stores the average value of all the values in the series.

However, when I debug the function, I see that series[0] has the values {totnr= -858993360 outnr= -858993459 average= -107374176.}

series[1] and so on all have similar values. I really need help with this, as I can't see why this is occurring.

Here in the contents of the text file the function is reading from: https://justpaste.it/16orx

int read_data(FILE *tsin, struct seriespost series[])
{
  int i = 0;
  float total = 0, number = 0;


  i = 0;
  while (fscanf(tsin, "%f", &number) != EOF)
  {
    total = 0;

    do {
      fscanf(tsin, "%f", &number);
      total = total + number;
      series[i].totnr = series[i].totnr + 1;
      if (number > 231 || number < 209)
      {
        series[i].outnr++;
      }

    } while (number != 0.0);
    i++;
    series[i].average = (total) / (series[i].totnr);
  }
  return i;
}

“但是,当我调试函数时,我看到series [0]的值为{totnr = -858993360 outnr = -858993459 average = -107374176。}”您需要使所有数组都从0开始。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM