Thread: Bowling Program

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

    Bowling Program

    Ok for my Programming II class there is an assigment that is supposed to pull a list of scores from a file and calculate them in output back to the user. There should be a prompt that asks for the file (an error message if there is no file). The program then uses the list of numbers and calculates the total score at the end.

    To start off though I need help with the following code specifically in the "// Read N, how many numbers " part.
    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
     
    
    int main()
    
    {
    
    // Declare file pointer
    
    FILE *ifp;
    
    int N, i, x;
    
    char fname[50];
    
     
    
    printf("Enter filename: ");
    
    scanf("%s", fname);
    
    printf("Opening %s\n", fname);
    
     
    
    // Open file
    
    ifp = fopen(fname, "r");
    
    if (ifp == NULL)
    
    {
    
      printf("Error, %s not available\n", fname);
    
      exit(1);
    
    }
    
     
    
    // Read N, how many numbers
    
    fscanf( ifp, "%d", &N);
    
    printf("How many is %d\n", N);
    
    
    // Read the values from the file 
    
     for (i = 0; i < N; ++i)
    
    {
    
       fscanf(ifp, "%d", &x);
    
       printf("%d: %d\n", i, x);
    
    }
    
    return 0;
    -----
    In the output the program only prints 4 of the numbers in the file being opened in the program. Any advice?

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    How is the input file formatted? Give us an example.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

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

    File Example

    2
    3
    6
    5
    7

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I don't see any problems with your code.

    You do realize that in the example you gave us, only first 3 lines would be read? First line to determine how many lines to read and then second and third because the first line was 2.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

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

    Mistake

    Haha yah, I looked it over and it was just a stupid mistake. Ok going further with this though...I'm trying to make a bowling score program. There are 21 frames total in a game of bowling. Right now I am just trying to add these 21 numbers together one after another...

    Code:
    // Read N, how many numbers
    
    fscanf( ifp, "%d", &N);
    
    printf("How many is %d\n", N);
    
    
    // Read the values from the file 
    
     for (i = 0; i < N; ++i)
    
    {
       int total;
       fscanf(ifp, "%d", &x);
       total = total + x;
       printf("%d\n",  x);
       printf("%d\n", total);
    }
    Here's the file it's pulling from...
    Code:
    21
    
    2
    1
    
    4
    5
    
    7
    6
    
    8
    9
    
    2
    3
    
    7
    5
    
    6
    0
    
    1
    3
    
    1
    6
    
    3
    9
    8
    The output i'm trying to achieve is this set of numbers but after each number I want the sum of it and the numbers before it...like in bowling. I'm excluding spare and strike rules at the moment. The output starts adding at 1628480098...where did I go wrong?
    Last edited by DuckCowMooQuack; 01-24-2011 at 02:51 PM. Reason: Addition information

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you believe total starts at zero?

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You did not initialize total, therefore it starts out with whatever value happened to be at the memory location it was put at.

    Think of defining a variable as drawing a box on a whiteboard that already has writing all over it. Chances are, you won't find a clean spot to draw a box of the desired size, so you draw over whatever is there already. When you initialize the variable - give it an initial value - you're erasing everything that got caught inside the box when you drew it and writing in the value you want.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Here's my complete code so far...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    
    {
    
    // Declare file pointer
    
    FILE *ifp;
    
    int N, i, x;
    
    char fname[50];
    
    printf("Enter filename: ");
    
    scanf("%s", fname);
    
    printf("Opening %s\n", fname);
    
    // Open file
    
    ifp = fopen(fname, "r");
    
    if (ifp == NULL)
    
    {
    
      printf("Error, %s not available\n", fname);
    
      exit(1);
    }
    
     
    // Read N, how many numbers
    
    fscanf( ifp, "%d", &N);
    
    printf("How many is %d\n", N);
     
    // Read the values from the file 
     
     int total = 0;
    
     for (i = 0; i < N; ++i)
       
    { 
       fscanf(ifp, "%d", &x);
       printf("%d\n", x);
       total = total + x;
       printf("The total is: %d\n", total);
    }
    
    return 0;
    
    }
    Here is the file it is pulling from...It's supposed to represent a bowling score, each set of two numbers representing a frame (the set of three at the end representing the 10th frame).
    Code:
    21
    
    2
    1
    
    4
    5
    
    7
    6
    
    8
    9
    
    2
    3
    
    7
    5
    
    6
    0
    
    1
    3
    
    1
    6
    
    3
    9
    8
    Now, I have to incorporate STRIKE (10 plus two frames) rules and SPARE(10 plus 1 frame) rules. How would I do that? I've messed around with the for loop below but can't seam to get it to work.
    Code:
     int total = 0;
    
     for (i = 0; i < N; ++i)
       
    { 
       fscanf(ifp, "%d", &x);
       printf("%d\n", x);
       total = total + x;
       printf("The total is: %d\n", total);
    }

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    This depends on how you are storing a spare or strike in your input file. Will you use the conventional 'X' and '/' ? That will break your current input gathering routine.

    Will your input file be shorter if somebody bowls a perfect game? In a perfect game, only 12 balls are thrown, all strikes. Will your input file start with a 12 and have 12 X's or 10's?

    Can you give us an example input file with spares and strikes and only valid bowling scores? You have several frames in your example with invalid score combinations (e.g. 7, 6; 8, 9; 7, 5; 3, 9, 8).

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Spares and strikes will not be represented with X's and /'s. Here is an example Program of what i'm trying to do.

    FILE
    Code:
    1 0 1 9 2 2 10 3 3 10 1 9 3 7 10 1 2
    Output of program...
    Code:
    B O W L I N G   S T U F F            
    
    =================================================
    
    Frame  1:  1   total   1
    
    Spare
    
    Frame  2: 12   total  13
    
    Frame  3:  4   total  17
    
    Strike!
    
    Frame  4: 16   total  33
    
    Frame  5:  6   total  39
    
    Strike!
    
    Frame  6: 20   total  59
    
    Spare
    
    Frame  7: 13   total  72
    
    Spare
    
    Frame  8: 20   total  92
    
    Strike!
    
    Frame  9: 13   total 105
    
    Frame 10:  3   total 108
    
    =================================================

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, so you will have somewhere between 12 and 21 numbers in your file depending on how many strikes were thrown (excluding the first number telling you how many balls were thrown). I would define an array of 21 ints, and initialize them all to zero. Then, I would read in the scores ball by ball into the array.

    Once you have all your input, you can do something like the following:
    Code:
    frame = 1
    total = 0
    for ball from 0 to num_balls_thrown - 1
        if the score for this ball is 10
            frame_score = add current and next two balls
            print "strike"
            increment i by 1
        else if the score for this ball and the next ball total 10
            frame_score = add current and next two balls
            print "spare"
            increment i by 2
        else
            frame_score = add current and next ball
            increment i by 2
        total += frame_score
        print frame, frame_score and total
    Note that this might not work for the 10th frame since that's a bit special, but this should get you well on your way.

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

    Thanks!!!

    Quote Originally Posted by anduril462 View Post
    Okay, so you will have somewhere between 12 and 21 numbers in your file depending on how many strikes were thrown (excluding the first number telling you how many balls were thrown). I would define an array of 21 ints, and initialize them all to zero. Then, I would read in the scores ball by ball into the array.

    Once you have all your input, you can do something like the following:
    Code:
    frame = 1
    total = 0
    for ball from 0 to num_balls_thrown - 1
        if the score for this ball is 10
            frame_score = add current and next two balls
            print "strike"
            increment i by 1
        else if the score for this ball and the next ball total 10
            frame_score = add current and next two balls
            print "spare"
            increment i by 2
        else
            frame_score = add current and next ball
            increment i by 2
        total += frame_score
        print frame, frame_score and total
    Note that this might not work for the 10th frame since that's a bit special, but this should get you well on your way.
    Thank you for this helpful information!!! I'm gonna keep on coding away.

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