The program written takes a list of numbers from a file and then adds them using bowling scores (strikes, spares). It should print out the number of balls thrown (number of numbers on file excluding -1), and then it should print out the total score update per frame.
Ok so I pretty much finished this but I keep getting a segmentation fault...Here's my code and the file it's pulling from. If someone can find the fix for the Sementation Fault I would gladly appreciate it!
CODE:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *ifp;
int N, x;
char fname[21];
printf("Enter filename: ");
scanf("%s", fname);
printf("Opening %s\n", fname);
ifp = fopen(fname, "r");
if (ifp == NULL)
{
printf("Error, %s not available\n", fname);
exit(1);
}
// Read N, how many numbers
int scores[21];
int i, y;
for(i = 0; i<21; ++i)
{
fscanf( ifp, "%d", &y);
if(y == -1)
{
break;
}
scores[i] = y;
printf("%d\n", y);
}
int f,e,total;
y = scores[0];
e = 0;
total = 0;
while(y != -1)
{
if(scores[e] == 10)
{
total = 10 + scores[e+1] + scores[e+2];
printf("The total is: %d", total);
e = e + 1;
}
else if(scores[e] + scores[e+1] == 10)
{
total = 10 + scores[e+2];
printf("The total is: %d", total);
e = e + 2;
}
else
{
total = scores[e] + scores[e+1];
printf("The total is %d", total);
e = e + 2;
}
}
return 0;
}
FILE:
Code:
3
4
4
5
3
6
0
9
7
3
2
5
6
4
10
1
6
3
6
-1
Now this program excludes the final frame. It ignores the 10th frame having three chances. Ignoring that, can someone help me find why there is a segentation error?