0

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);

    }
Coral Doe
  • 1,925
  • 3
  • 19
  • 36
Luca
  • 443
  • 2
  • 8
  • 17
  • 3
    Step through your code with a debugger to locate the problem, then post the relevant code here. – mathematician1975 Aug 05 '12 at 15:37
  • If you are certain that the code crashes in the loop you could post the code of your loop – mathematician1975 Aug 05 '12 at 15:39
  • loop has been added, i'm not sure if that's the issue though. – Luca Aug 05 '12 at 15:42
  • You are doing a lot of accessing of array elements using a number of different index variables. Have you stepped through this with a debugger to make sure you are not accessing any elements beyond the bounds of your arrays?? – mathematician1975 Aug 05 '12 at 15:49
  • no i haven't. I'm a 1st year student so I have no idea how to use a debugger. – Luca Aug 05 '12 at 15:51
  • 2
    This is the first thing you do when you have code problems. You are using visual studio you say - check out any help documentation to tell you how to set breakpoints and how to step through code line by line. This allows you to execute your code a line at a time, which allows you to see the values of your variables and helps identify bugs. It will also allow you to locate where your access violation occurs. Using a debugger is vital for programming - it is worth taking the time to learn how to do it. – mathematician1975 Aug 05 '12 at 15:55
  • In the second part of the loop, you use `j` for the indexing, `percentage[j]`, but you're processing `student_id[i]`, and after the status bar has been filled, `j` is `l`, which is `percentage[i]/10` (probably +1, since the percentage rarely would be a multiple of 10). There's a slim possibility that that's how it is intended, but more likely that makes `percentage[j]` an out-of-bounds access, which could be your access violation. – Daniel Fischer Aug 05 '12 at 16:38

1 Answers1

2

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 %%.