-3

I have an issue with my code but I have no idea what is wrong with it, here my code:

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

int main() {
    char M[1000];//stores the sex
    int age[1000];//stores the age
    char np[1000];//stores whether negative or positive
    char ch, cn;
    int h, w, z;
    float am = 0, af = 0;
    char st;
    int i = 0, j = 0, k = 0, l = 0;//i for number of males, j for number of females
    int ip = 0, jp = 0;//ip-> number of positive males, jp->no of positive females
    int young = 0, old = 0;//youngest and oldest person

    FILE *covid;
    covid = fopen("HW #1 Data.txt", "r");

    if (covid == NULL) {
        printf("Error opening file\n");
        return 1;
    }

    while (!feof(covid)) {
        ch = getc(covid);//reading sex
        M[k] = ch;
        ch = getc(covid);//reading space
        fscanf(covid, "%d", &age[k]);//reading age to the array
        ch = getc(covid);//reading space
        fscanf(covid, "%d", &h);//reading height
        ch = getc(covid);//reading space
        fscanf(covid, "%d", &w);//reading weight
        ch = getc(covid);//reading space
        ch = getc(covid);//reading negative/positive
        np[k] = ch;//assigns to the array
        fscanf(covid, "%d", &st);//reading zip
        ch = getc(covid);//reading end of line
        k++;
    }

    young = age[0];
    old = age[0];

    for (l = 0; l < k; l++) {
        if (M[l] == 'M') {
            i++;
            if (np[l] == '+') {
                ip++;
                am = am + age[l];
            }
        } else if (M[l] == 'F') {
            j++;
            if (np[l] == '+') {
                jp++;
                af = af + age[l];
            }
        }

        if (age[l] < young) {
            young = age[l];
        }
        if (age[l] > old) {
            old = age[l];
        }
    }

    printf("\n%d males have been tested and %d have been tested positive", i, ip);
    printf("\n%d females have been tested and %d have been tested positive", j, jp);
    am = (float) am / ip;
    af = (float) af / jp;
    printf("\nAverage age of postive men is %f", am);
    printf("\nAverage age for postive female is %f", af);
    printf("\noldest is %d", old);
    printf("\nYoungest is %d", young);

    fclose(covid);

    return 0;
}

My output is:

395 males have been tested and 38 have been tested positive
355 females have been tested and 30 have been tested positive
Average age of positive men is 51.868420
Average age for positive female is 54.433334
Oldest is 6420633
Youngest is 12

The oldest is supposed to be 97. I don't know what I'm doing wrong.

The youngest had no issues and I checked the file and there is nothing wrong with everything is fine except the old variable.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

1

tl;dr: Check the value returned by scanf, and use feof correctly.

The problem (as mentioned in multiple comments!) is related to data input. However, the error with your use of feof is not really the core of the problem. (Although it must be fixed.) IMO, the fundamental error here is the failure to check the value returned by scanf. In particular, on the last iteration of the loop, the line fscanf(covid,"%d",&age[k]); fails to read any data (because you are using feof incorrectly), but you didn't notice because you didn't check if fscanf returned 1. So age[k] is not changed, but retains its uninitialized state. When that value is later read, that is undefined behavior and anything can happen. (Practically, it means that 6420633 was the garbage value on the stack that happened to be in that memory location that was never initialized but was read from when computing the oldest value.)

The simplest way to fix the issues is to use the far more idiomatic:

while( 6 == fscanf(covid, "%c%d%d%d %c%c ",                                        
                M + k, age + k, &h, &w, np + k, &st) ){   
        ...                         
        k += 1;                                                                    
}

(Since you've provided no sample input, I can't guarantee that I've translated your code to the correct format string, but that gives you the idea.)

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • The false final read would have retained the values previously in the variables, except the arrays, and the age is an array element. Another reason for error is passing the address of a `char` for `%d` and corrupting another variable. – Weather Vane Nov 25 '20 at 13:41