#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]);
//void calcData(SKATER skater[MAX]);
int main (void)
{
// Global Declarations
SKATER skater[MAX_SKATER];
int num;
// Function calls
num = getData(skater);
//calcData(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, 100, fpIn))
{
sscanf(buffer,"%*c%19[^0123456789]", skater[i].name);
puts(buffer);
for(k = 0; k < MAX_BASE; k++)
{
sscanf(buffer,"%d %f", &skater[i].elements, skater[i].baseval);
for(j = 0; j < COLUMN_SCORE; j++)
{
sscanf(buffer,"%d", &skater[i].score[k][j]);
}
}
i++;
}
system("PAUSE");
fclose(fpIn);
return i;
}
This getdata function is to read the data from a file to arrays of structure.The first line contain the name, the next line contain the elements number the base value and the rest of of the numbers is the score, this gets repeat 8 time. The getdata function only read the first 3 line of data include the name 2 other line when i try puts(buffer) why do i get this problem can anyone tell me how to fix it ? Am i having any logical error?
this is the data :
MENS SHORT PROGRAM//http://www.isufs.org/results/owg2006/OWG06_Men_SP_Scores.pdf
LYSACEK Evan
1 7.5 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3
2 10.0 1 2 2 1 1 1 1 2 0 1 1 1
3 3.0 1 2 2 2 1 0 1 2 1 1 1 2
4 3.1 1 1 1 1 1 0 0 0 1 0 1 0
5 1.7-3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3
6 2.1 0 0 1 1 1 1 1 1 1 1 1 1
7 3.1 0 0 1 1 0 0 1 1 1 1 1 1
8 3.5 0 1 1 2 1 1 1 1 1 1 0 1
WEIR Johnny
1 7.5 2 2 2 2 1 1 1 1 1 2 1 1
2 10.0 1 1 1 1 2 0 1 1 1 1 2 1
3 3.0 1 1 1 2 1 0 1 1 2 2 2 2
4 3.1 1 2 1 2 1 1 0 0 2 1 1 0
5 5.5 0 -1 0 -1 -1 0 -1 -1 1 -2 -2 -2
6 1.3 1 1 1 2 1 1 1 0 1 1 1 2
7 3.1 0 1 1 1 1 0 0 1 2 1 1 1
8 3.0 -1 1 1 2 1 0 1 0 2 1 -1 1
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
SAVOIE Matthew
1 3.0 0 0 0 1 0 0 0 0 0 0 0 -1
2 7.5 1 2 2 1 1 1 1 1 1 1 2 2
3 9.5 0 1 1 0 0 0 0 0 0 0 1 1
4 3.1 1 1 1 1 1 1 0 0 1 1 0 0
5 1.9 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3
6 2.1 0 0 1 0 1 0 0 1 1 1 1 1
7 3.1 0 0 1 0 0 0 1 1 2 0 2 1
8 3.0 0 0 1 1 1 0 1 1 1 1 1 1
Since it is homework I will only give you some hints on what you have going wrong. I hope this gets you back on the right track! 🙂
You mention that you are only reading in a limited number of lines. You should look to your loop condition, “while(i < MAX_SKATER && fgets(“, to see why it is only printing a few of your lines.
I will give you this one since you are just starting. In your sscanf call to read in the score line, every parameter must be passed as a pointer. The second variable you pass in to receive data isn’t being passed correctly. Instead use “&skater[i].baseval”. The same goes for your player name sscanf.
You will need to call fgets again, likely in your “for(k” loop. As you have it, you are only attempting to read in your header line. Then you are re-using the same buffer data (a header line) when trying to parse a score line.
You will need to separate your sscanf in your “for(j” loop, resulting in a new sscanf in your “for(k” loop. You will have to read in the player number and baseval separate from the scores because you have a variable number of scores to read (COLUMN_SCORE).