Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct data_struct record;
typedef record *link;
struct data_struct
{
int id;
char fname[25];
char lname[25];
int height;
int age;
char bowlframes[25];
int bowlscore;
link next;
};
link newrecord( int id1, char fname1[], char lname1[], int height1, int age1, char bowlframes1[], int bowlscore1)
{
link tmp;
tmp = malloc( sizeof(record) ); //allocates a box with fname
tmp->id = id1;
strcpy(tmp->fname, fname1);
strcpy(tmp->lname, lname1);
tmp->height = height1;
tmp->age = age1;
strcpy(tmp->bowlframes, bowlframes1);
tmp->bowlscore = bowlscore1;
tmp->next = NULL;
return tmp; //returns the amount address of memory to the main
}
link insertrear ( link head, link p )
{
link tmp;
if (head == NULL)
head = p;
else
{
tmp = head;
while ( tmp->next != NULL )
tmp = tmp->next;
tmp->next = p;
}
return head;
}
void printstuff( link t )
{
link shift = t;
while (shift != NULL)
{
printf("ID: %d\n", shift->id);
printf("%s ", shift->fname);
printf("%s\n", shift->lname);
printf("Height: %d\n", shift->height);
printf("Age: %d\n", shift->age);
printf("Bowling Frames: %s\n", shift->bowlframes);
printf("Final Score: %d\n\n", shift->bowlscore);
shift = shift->next;
}
}
int computescore(char scores[], int len)
{
char num = ' ';
int frame[30];
int b, y, x, total;
x = 0;
for(b = 0; b < len; b++)
{
num = scores[x];
if(num == 'X')
{
frame[x] = 10;
x++;
}
else if(num == '/')
{
frame[x] = 10 - frame[x-1];
x = x + 2;
}
else if(num >= '0' && num <= '9')
{
frame[x] = num - '0';
x++;
}
}
total = 0;
x = 0;
b = 0;
for(b = 0; b < len; b++)
{
if(frame[x] == 10)
{
total = total + frame[x+1] + frame[x+2] + 10;
x++;
}
else if(frame[x] + frame[x+1] == 10)
{
total = total + 10 + frame[x+2];
x = x + 1;
}
else
{
total = total + frame[x] + frame[x+1];
x = x + 2;
}
}
return total;
}
int main()
{
link L, p, q;
int id, height, age, bowlscore, len, score1;
char fname[30], lname[30], bowlframes[30];
L = NULL; //pointer to NULL
FILE *fp;
fp = fopen("in.txt", "r");
if (fp == NULL)
{
printf("Can't open file, check director.\n");
exit(1);
}
fscanf(fp, "%d", &id);
int N = 0;
int u = 0;
while( !feof(fp) )
{
fscanf(fp, "%s", fname);
fscanf(fp, "%s", lname);
fscanf(fp, "%d", &height);
fscanf(fp, "%d", &age);
fscanf(fp, "%s", bowlframes);
len = strlen( bowlframes );
score1 = computescore(bowlframes, len);
p = newrecord(id, fname, lname, height, age, bowlframes, score1);
L = insertrear( L, p);
N++;
u++;
fscanf(fp, "%d", &id);
}
printstuff( L );
return 0;
}
FILE:
Code:
int computescore(char scores[], int len)
{
char num = ' ';
int frame[30];
int b, y, x, total;
x = 0;
for(b = 0; b < len; b++)
{
num = scores[x];
if(num == 'X')
{
frame[x] = 10;
x++;
}
else if(num == '/')
{
frame[x] = 10 - frame[x-1];
x = x + 2;
}
else if(num >= '0' && num <= '9')
{
frame[x] = num - '0';
x++;
}
}
total = 0;
x = 0;
b = 0;
for(b = 0; b < len; b++)
{
if(frame[x] == 10)
{
total = total + frame[x+1] + frame[x+2] + 10;
x++;
}
else if(frame[x] + frame[x+1] == 10)
{
total = total + 10 + frame[x+2];
x = x + 1;
}
else
{
total = total + frame[x] + frame[x+1];
x = x + 2;
}
}
return total;
}
It gives me a random number for total. I know this means it's accessing some random point in memory but I don't know why it goes outside of the array/string. Can anyone help me figure it out?