Thread: Bowling Program Re-Visited

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    55

    Bowling Program Re-Visited

    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?
    Last edited by DuckCowMooQuack; 01-25-2011 at 09:44 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Before the while loop you set y = scores[0]. Then you use the value of y to control the loop. But does the value of y ever change? (No.) So e keeps increasing until scores[e] ends up outside your program's space.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    while(e <= scores[0])
       {
    if(scores[e] == 10)
       {
       total = 10 + scores[e+1] + scores[e+2];
       printf("The total is: %d", total);
       e = e + 1;

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by CommonTater View Post
    Code:
    while(e <= scores[0])
       {
    if(scores[e] == 10)
       {
       total = 10 + scores[e+1] + scores[e+2];
       printf("The total is: %d", total);
       e = e + 1;
    ???

    True, it does stop the segfault, but how does it make any sense to use scores[0] (the first score entered) to limit the number of iterations?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... at one point I thought you said the first entry in the file was the number of entries in the file... Then you set y to be scores[0] and used while (y != -1) ... so I just took that to mean that you wanted scores[0] to control your loop...

    If there are always the same number of entries you should use a for() loop...
    If not, then you need to count entries and again use a for() loop

    The seg fault was because you basically have an infinite loop that only exits when you overrun your array boundaries and cause a system fault.
    Last edited by CommonTater; 01-25-2011 at 11:46 PM.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Uh, not me. I'm just the guy who answered that question a few posts back...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by R.Stiltskin View Post
    Uh, not me. I'm just the guy who answered that question a few posts back...
    Ahhh... sorry... The way I do things is that, if I'm speaking generally or in response to the topic in general I don't quote anyone in my messages. When I'm speaking directly to someone I will quote the relevent parts of their messages...

    Sorry for the confusion...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a bowling program...
    By fuze in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2008, 04:06 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread