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

#define MAX 20
#define MAX_BASE 8
#define ROW 9
#define COLUMN_SCORE 12
#define MAX_SKATER 4

typedef struct{
            char  name[MAX];
            int   elements;
            float baseval[MAX_BASE];
            int score[12][MAX_BASE];
            double total_score;
          }SKATER;

int getData(SKATER skater[MAX]);


int main (void)
{
    // Global Declarations
    SKATER skater[MAX_SKATER];
    int num;

    // Function calls
    num = getData(skater);

return 0;
}

/********************************* getData ************************************
Pre:
Post:
*/
int getData(SKATER skater[MAX_SKATER])
{
    // LOcal Declarations
    FILE* fpIn;
    int   i = 0;  
    int   k;
    int   j;
    char  buffer[100]; 

    // Statements
    if((fpIn = fopen("lab6data.txt","r"))==NULL)
    {
    printf("File opening error");
    system("PAUSE");
    exit(100);
    }

    while(i < MAX_SKATER && fgets(buffer, sizeof(buffer) - 1, fpIn))
    {
         sscanf(buffer,"%*c%19[^0123456789]%[^\n]", skater[i].name);    
         for(k = 0; k < MAX_BASE; k++)
             printf("loop");
             sscanf(buffer,"%d", &skater[i].elements, skater[i].baseval);
             for(j = 0; j < COLUMN_SCORE; j++)
             {
                sscanf(buffer,"%d", skater[i].score[k][j]);
             }
        i++;        
    }

    fclose(fpIn);
    return i;
}

Hi, When I tried to debug this code it shows me the error that buffer was overrun, how do I fix this error? This is the first time that I've encounter this error, also, I'm not sure if the way that I approach this problem was right? can anyone tell me a way to approach the problem so here is the sample data for the problem:

PLUSHENKO Evgeni
1 13.0 0 2 1 1 1 0 1 0 1 1 1 1 
2 7.5 1 2 2 2 2 1 2 1 2 2 2 2
3 6.0 2 1 1 1 1 0 0 2 1 2 1 2
4 2.3 2 1 1 1 1 1 2 1 1 1 1 1
5 3.4 2 2 2 2 1 2 3 3 2 3 2 1
6 2.1 1 1 1 2 2 0 0 0 1 2 1 1
7 3.1 1 0 2 2 1 1 1 2 2 2 2 1
8 3.5 1 1 2 2 1 1 1 1 2 2 1 1  

There are 3 more blocks of data beside this one thank for helping

2
  • 4
    Looks like sscanf(buffer,"%d", skater[i].score[k][j]); should be sscanf(buffer,"%d", &skater[i].score[k][j]); (you're missing an &). Commented Mar 18, 2012 at 6:59
  • I am not sure about what the program does. But as @DCoder says, when you add the miss the missing & everywhere, program runs normally Commented Mar 18, 2012 at 7:08

1 Answer 1

2

You define int score[12][MAX_BASE]; in the struct SKATER.But you scanf like this:

         for(j = 0; j < 12; j++)
         {
            sscanf(buffer,"%d", skater[i].score[k][j]);
         }

The index is error.You should change to sscanf(buffer,"%d", &skater[i].score[j][k]); and don't miss &

sscanf(buffer,"%d", &skater[i].elements, skater[i].baseval); change to sscanf(buffer,"%d %f", &skater[i].elements, skater[i].baseval);

I think you need read this.Need passed the point to sscanf

Good Luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.