Thread: Newbie Help

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Newbie Help

    hi guys i need help,

    Write a program that reads a data file and uses an array to figure out bowling scores.

    In bowling, 10 pins are set up for each frame and the player gets two chances to knock them down.

    1. If the player knocks down less than 10 ten pins down with the two balls, his/her score for that frame is the total number of pins knocked down.

    2. If the player knocks down all 10 pins with the two balls, it is called a spare and is marked with a "/"). The player gets 10 points plus the number of pins knocked down with the next ball.

    3. If the player knocks down all 10 pins with one ball, it is called a strike and is marked with an "X"). The player gets 10 points plus the number of pins knocked down with the next two balls.

    Frame 10 is a special case. If you get a strike on the first ball you get two extra balls to add-up their scores. If you get a spare with the first two balls then you get a third ball to throw. If you fail to knock down all pins with the first two balls, the game ends there.

    Input data: The data file contains the player’s name and the number of pins knocked down with a ball. -1 indicates the end of the game (the next record is another player name).

    For each player, print out the the traditional bowling sheet for that player.
    Print out at the end the name of the winner and your personal and copyright information (see example).

    The output report should be like the following example. This is for only one player; your report must include all players (skip 3 lines between players in the report).

    Your report must use the data from the bowling.dat file but should work with any valid game file.



    Here is what I got:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define TRUE 1
    #define FALSE 0
    
    void print_date_and_time()
    {
      time_t t;
      char now[20];
      time(&t);
      strcpy(now, ctime(&t));
      printf("%s", now);
    }
    
    
    
    void calculate_and_print(FILE *fp)
    {
      char *first_name, *last_name;
      int scores[21], points[12];
      int i, frame, first_try = TRUE;
      int strike = FALSE, spare = FALSE;
    
      printf("Frame\t\t 1   2   3   4   5   6   7   8   9   10\n");
    
      while(1)
      {
        /* initialize points array with 0 */
        for(i = 0; i < 12; i ++)
        {
          points[i] = 0;
        }
    
        /* Read first name and last name */
        fscanf(fp, "%s", first_name);
        if(!strcmp(first_name, "End"))
          break;
        fscanf(fp, "%s", last_name);
    
        printf("%s\t", last_name);
    
        /* store scores into array & calculate score*/
        i = 0;
        while(1)
        {
          fscanf(fp, "%d", &scores[i]);
          if(scores[i] == -1)
            break;
          i++;
        }
    
        frame = 0; /* used as index for points array (frame 1 = 0) */
        i = 0;     /* used as index to access scores array */
    
        while(1)
        {
          if(scores[i] == -1)
            break;
    
          if(first_try)
          {
            if(scores[i] == 10)  /* strike */
            {
    	  points[frame] = 10;
              strike = TRUE;
              frame++;
              first_try = TRUE;
              printf("X  ");
            }
            else 
            {
              points[frame] = scores[i];
              first_try = FALSE;
              printf("%d", scores[i]);
            }
          }
          else  /* second try */
          {
    	points[frame] = points[frame] + scores[i];
    
            if(strike)  /* if strike in pervious frame */
            {
              points[frame - 1] = points[frame - 1] + points[frame];
              strike = FALSE;
            }
    
            if(spare) /* if spare in previous frame */
            {
              points[frame - 1] = points[frame - 1] + scores[i - 1];
              spare = FALSE;
            }
    
            if(scores[i - 1] + scores[i] == 10) /* spare */
            {
              spare = TRUE;
              printf("/  ");
            }
            else
              printf("-%d  ", scores[i]);
    
            frame++;
            first_try = TRUE;
          }
          i++;
        }
    
        /* Accumulate scores */
        for(i = 1; i < 10; i ++)
        {
          points[i] = points[i - 1] + points[i];
        }
    
        /* Handle 10th frame case */
        points[8] = points[9];
        points[9] = points[9] + points[10] + points[11];  /* additional scores 
    if there's any */
    
        printf("\nScore:\t\t");
        printf("%d  ", points[0]);
        for(i = 1; i < 10; i ++)
          printf("%d  ", points[i]);
        printf("\n\n\n");
    
    printf("Program by:bla bla and bla bla\n");
    printf("We worked in the partners\n");
      }
    }
    
    
    int main(void)
    {
      FILE* fp;
      char *file_name;
    
      printf("Enter data file name: ");
      scanf("%s", file_name);
      fp = fopen(file_name, "r");
      if(fp == NULL)
      {
        printf("Can't open %s\n", file_name);
        exit(1);
      }
    
      calculate_and_print(fp);
      getch();
      return 0;
    }
    I don't see any time. Do I have to call the funtion time in the main? Please any help is arprecaited. Thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    When your function is 4 times the size of main and contains 90% of the input and output, you're usually doing something wrong.

    As for seeing the time, you have to call your print_date_and_time() function somewhere. Why you would call it in main I don't know since most of your program is in your function, but it's gotta be called.
    Last edited by SlyMaelstrom; 12-05-2005 at 07:03 PM.
    Sent from my iPad®

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Correction, make that FOUR people asking about the same assignment.

    > Do I have to call the funtion time in the main?
    Yes.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Salem
    Correction, make that FOUR people asking about the same assignment.
    ...and it still remains ZERO that have yet to impress me.

    I wonder if their teacher gave them the link to this website.
    Sent from my iPad®

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int main(void)
    {
      FILE* fp;
      char *file_name;
    
      printf("Enter data file name: ");
      scanf("%s", file_name);
    Either malloc() a string or use an array for that string.

    [edit]
    Same here:
    Code:
    void calculate_and_print(FILE *fp)
    {
      char *first_name, *last_name;
      int scores[21], points[12];
      int i, frame, first_try = TRUE;
      int strike = FALSE, spare = FALSE;
    
        /* ... */
    
        /* Read first name and last name */
        fscanf(fp, "%s", first_name);
    [/edit]
    Last edited by dwks; 12-06-2005 at 11:46 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM