0

Here the if statement inside the while loop should return the matching GPA holders. like how many students got GPA 10, but instead, it returns the same value twice along with the other values.

input: name: a, total gpa: 10 name : b, total gpa: 10 name :c, total gpa: 10

output: abac

void findGPATenHolders(int size)
{
    Student Student3[100];
    FILE *fp;
    fp = fopen("students.txt", "r");

    if (fp == NULL)
    {
        printf("File not found \n"); // this will be printed if we change data.txt to data1.txt
    }
    else
    {
        printf("*********Total GPA Analysis********\n");
        int i=0;
       
        while (fscanf(fp, "%d %s %lf %lf %lf\n", &Student3[i].id, &Student3[i].name, &Student3[i].totalGPA, &Student3[i].hscGPA, &Student3[i].sscGPA) != EOF)
        {   int j = 0; 
            
              printf("GPA %.2f Students:", (float)Student3[i].totalGPA);
             if (Student3[j].totalGPA == Student3[i].totalGPA && j != i)
                {   
                    printf("%s\n", Student3[j].name);       
                    
                }
                else
                {
                  
                    printf("Not working");
                }
            i++;
            j++;
        }
        
    }
    fclose(fp);
}
Ðаn
  • 10,934
  • 11
  • 59
  • 95
  • 2
    This is C++, not C The C language does not have member functions within structs. – PaulMcKenzie Aug 05 '21 at 15:54
  • Don't use `fflush(stdin)`: https://stackoverflow.com/questions/2979209/using-fflushstdin – William Pursell Aug 05 '21 at 15:56
  • 1
    You *must* limit the input sizes. At the very least, `scanf("%19s", Student1[i].name);` (and similarly for all inputs). Also, you need to check the return value of all the `scanf` calls during input. – William Pursell Aug 05 '21 at 15:59
  • `while (fscanf(...) != EOF)` is wrong, somewhat like [`while (!feof(...))` is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – pmg Aug 05 '21 at 16:10
  • any other suggestion to the for-loop inside the while? I have printed all the values using the while loop, it all printed succesfully. But the if condition does not work – Fatin Ishraq Aug 05 '21 at 16:43
  • Help me, I do not see a for loop within a while loop. – Yunnosch Aug 05 '21 at 16:59
  • "sometimes it returning one extra value" please provide a [mre] which demonstrates that. Provide the smallest input which triggers it. – Yunnosch Aug 05 '21 at 17:01
  • Your description of the problem is vague and hard to understand. Are you asking about a comparison that doesn't work or is the problem an extra value? Also, there are numerous bugs in the code. Can you pick *one* specific problem, remove all the code not related to that specific problem, and show the input, expected output, and actual output? – David Schwartz Aug 05 '21 at 17:11
  • @FatinIshraq Don't you need two loops? One to get the input and another to check each input against all prior inputs? Your code only has one loop. – David Schwartz Aug 05 '21 at 17:42
  • I have checked two loops also, the result stays the same. I followed the concept of finding duplicate values in an array. – Fatin Ishraq Aug 05 '21 at 17:51
  • You always compare the new read record with the first read record! It is because the j counter is always set with zero! Do you want to check with the first one? @FatinIshraq – G. Emadi Aug 05 '21 at 17:57
  • What does the `size` parameter do? Why are you comparing two different `Student3` if you want to find the 10? What format should in be in (`malloc` line numbers? That's going to be a problem.) – Neil Aug 05 '21 at 18:09
  • size parameter does how many times the loop should run – Fatin Ishraq Aug 05 '21 at 18:50
  • 1
    `while (fscanf(...)!= EOF)` so, you are fine if you canot parse any of the required parameters as long as you don't reach the end of file? Why not compare with `==5` if you expect 5 values? – Gerhardh Aug 05 '21 at 19:58
  • done that, but the problem is I think the logic of comparing two gpa – Fatin Ishraq Aug 06 '21 at 05:47
  • "it returns the same value twice along with the other values." because you always display the first item printf("%s\n", Student3[j].name); where j == 0 You should display Student3[i].name); – GRFRM Aug 06 '21 at 08:11

0 Answers0