1
int main()
{
    int* ptr;
    int numofstu;

printf("Please enter the number of students: \n");
scanf("%d", &numofstu);
ptr = (int*) malloc(numofstu * sizeof(int));

FILE* fptr;
if ((fptr = fopen("C:\\Users\\umut\\Desktop\\students.txt", "r")) == NULL)
{
    printf("Error! opening file");
}
else
{
    int highest=0;
    for (int i = 0; i < numofstu; i++)
    {
        fscanf(fptr, "%d\n", &ptr[i]);
        if (ptr[i] >highest )
            ptr[i] = highest;
        

    }
    printf("%d", highest);
}
}

Hello Guys! This is my second semester in C language but i'm still a amateur.

What is the problem of this code chunk. I am posting a new question because i coulnt found any similar resource to solve it?

Thanks in advance :)

Edit: There are 9 integers in students.txt and their order is like int newline int

Edit2: Result is 0 all the time.

Umut
  • 27
  • 3
  • 3
    Instead of `ptr[i] = highest;`, don't you mean `highest = ptr[i];`? – Andreas Wenzel Mar 17 '21 at 00:19
  • 1
    Just as a side note: It is generally a good idea to check the return value of `scanf` and `fscanf`, to verify that it was able to match an integer and write it to the specified location. For example: `if ( fscanf(fptr, "%d\n", &ptr[i]) != 1 ) {printf("error in fscanf\n"); exit(EXIT_FAILURE);}`. Note that this code requires you to `#include `. – Andreas Wenzel Mar 17 '21 at 00:22
  • In future, it should be possible for you to find such bugs yourself, by running your code line by line in a debugger while monitoring the values of all variables. See this question for more information on how to do this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Mar 17 '21 at 00:28

0 Answers0