I am trying to create a C program to calculate bowling scores (this is not the same as the other bowling problems posted here). I am having trouble figuring out where to go when a strike is encountered without using 1000 if statements. I would greatly appreciate any help i can get with either logic or code.
Here is a stripped down version of my code (no fancy display), it contains only the scoring functions. I have heard that a struct or a flag might help me, but i am not very familiar with them yet so i am heading to look them up now. Again, any help would be greatly appreciated. Thanks.
Code:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
int main(void)
{
FILE *fp;
char c;
int i = 0;
int j = 0;
int players = 0;
int frame[10][2];
int total_score = 0;
char strike_answer;
char spare_answer;
for(i = 0; i<10; i++)
{
for( j = 0; j<2; j++)
{
if( j == 0)
{
printf("Did You get a strike? y/n?");
strike_answer = getc(stdin);
fflush(stdin);
if(strike_answer == 'y')
{
printf("STRIKE!!!\n");
j = j+2;
// strike function
}
else
{
printf("Enter Score for Frame %i Ball %i\n", i+1,j+1);
scanf("%i", &frame[i][j]);
fflush(stdin);
total_score = total_score + frame[i][j];
printf("Total Score is %i\n", total_score);
}
}
if(j == 1)
{
printf("Did You get a spare? y/n?");
spare_answer = getc(stdin);
fflush(stdin);
if(spare_answer == 'y')
{
printf("spare\n");
// spare function
}
else
{
printf("Enter Score for Frame %i Ball %i\n", i+1,j+1);
scanf("%i", &frame[i][j]);
fflush(stdin);
total_score = total_score + frame[i][j];
printf("Total Score is %i\n", total_score);
}
}
}
}
system("pause");
return(0);