Unhandled exception at 0x77ea15de in Assignment 3.exe: 0xC0000005: Access violation reading location 0x33772c58

StackOverflow https://stackoverflow.com/questions/11817699

  •  24-06-2021
  •  | 
  •  

Pergunta

I don't understand what this error is telling me. When I run the program it should continuously loop until the user enters N (for no more/exit). I don't understand what is going on. After the first set of inputs, before the user is prompted "Would you like to process a new student" the program throws out that exception.

I've run it using Geany (which doesn't give me any error definition it just crashes), and Visual Studio 2010.

Can someone help?

void draw_bar_chart(int student_id[], int percentage[], int size)
{
    FILE *report;
    int i, j=0, min = 1000000, max = 0, min_pos, max_pos, l;
    char s[20];

    report = fopen("report2.txt", "a");
    fprintf(report, "\n******* BAR CHART (YEARLY TOTAL EXPENSES: YEARLY TOTAL INCOME) *******\n\n");      

    for (i = 0; i < size; i++)
    {
        fprintf(report, "%d%c", student_id[i], ' ');
        if (percentage[i] > 0){
            l = percentage[i]/10; //the lenght of the bar
            if ((percentage[i]%10) > 0)
                l++;
            for (j = 0; j < l; j++)
                s[j] = 'x';
            s[l+1] = '\0';
        } else {
            s[0] = '!';
            s[1] = '\0';
        }

        fprintf(report, "%-20s%6c%d%c\n", s, ' ', percentage[j], '%');

        if (percentage[j] >= 0)
        {
            if (percentage[j] < min)        
            {
                min = percentage[j];        
                min_pos = j;                
            }
            if (percentage[j] > max)    
            {
                max = percentage[j];        
                max_pos = j;                
            }
        }
    }


    fprintf(report, "***lowest percentage:  %d%c (student ID: %d)\n", min, '%', student_id[min_pos]);
    fprintf(report, "***highest percentage: %d%c (student ID: %d)\n", max, '%', student_id[max_pos]);

    fclose(report);

    }
Foi útil?

Solução

I can see the following errors:

  1. It should be s[l] = '\0', not s[l+1] = '\0'.
  2. After you write the bar in s, all occurences of j must be replaced with i.
  3. min_pos and max_pos might be uninitialized.

The real problem is number 2. You can avoid this kind of mistakes by getting in the habit of putting your variables in the smallest possible scope. i.e. had you written this:

        ...
        fprintf(report, "%d%c", student_id[i], ' ');

        /* display percentage */
        {
          char s[20];

          if (percentage[i] > 0) {
            int l, j;

            l = percentage[i] / 10; // the length of the bar
            if ((percentage[i]%10) > 0)
                l++;
            for (j = 0; j < l; j++)
                s[j] = 'x';
            s[l] = '\0';
          }
          else {
            s[0] = '!';
            s[1] = '\0';
          }

          fprintf(report, "%-20s%6c%d%c\n", s, ' ', percentage[i], '%');
        }

        /* update min, max */
        if (percentage[i] >= 0) {
        ...

Then the code would have been easier to follow and the compiler would have given you an error had you mistakenly used j instead of i after /* update ... */. Even better would be to put the percentage display bit in a separate function.

BTW you don't need these %c in the format strings, just put the characters in directly. % can be escaped with %%.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top