disc golf program troubles

This is a discussion on disc golf program troubles within the C Programming forums, part of the General Programming Boards category; Hey! I was trying to write a simple program to tally each players score in a game of disc golf, ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    disc golf program troubles

    Hey!

    I was trying to write a simple program to tally each players score in a game of disc golf, then display it at the end. Maybe there's a better approach I could use?

    Anyways, I keep getting junk numbers for the sums array. I set up an array for the amount of players, and the score per hole. I'm trying to add the holes to get a total, but I'm getting back junk. Help would be very much appreciated!

    Thank you!

    Here's the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main(){
        
        int holes[18]; 
        int players = 0;
        int i, i2;
        int h, g, f = 0;
        int x = 1;
        int sum = 0;
        int sums[players];
        sums[1] = 0;
        
        printf("You played a round of disc golf\nLet me add up the scores for you!\n");
        printf("Please enter the number of players: \n");
        scanf("%d", &players);
        for (i = 1; i <= players; ++i){ 
         printf("Enter the scores for player %d\n", x++);
          
          for (i2 = 0; i2 < 18; ++i2){
            scanf("%d", &holes[i2]);
            printf("%d\n", holes[i2]);}
            }
            
        for (g = 0; g < players; ++g){
            ++f; 
            for (h = 0; h < 18; ++h){ 
               printf ("%d\n", holes[h]); // trying to find where it goes wrong
               printf ("%d\n", sums[f]);
               system("PAUSE");
               sums[f] += holes[h];
               printf ("%d\n", holes[h]);
               printf ("%d\n", sums[h]);
               system("PAUSE");
               } 
                         }
                         
                         
         printf("this is %d", holes[5]);      
               
        for (i = 0; i < players; ++i)
          printf("\nplayer %d's score is %d\n", i + 1, sums[i]); 
          
          system("PAUSE");
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,392
    Code:
    int players = 0;
    ...
    int sums[players];
    Your problem would be with sums[]
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Quote Originally Posted by Click_here View Post
    Code:
    int players = 0;
    ...
    int sums[players];
    Your problem would be with sums[]
    Okay, I tried to just initialize it after players gets input

    Code:
    scanf("%d", &players);
    
    int sums[players];
    But that still gave me junk. Any ideas for a work around?

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,392
    Show your new code in full
    Fact - Beethoven wrote his first symphony in C

  5. #5
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Initialize the elements of the array

    Code:
        int sum[players];
        for (int k = 0; k < players; k++) sum[k] = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change maker program remainder troubles.
    By muffinman8641 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2011, 01:41 PM
  2. Program Troubles
    By dr0be in forum C Programming
    Replies: 7
    Last Post: 05-01-2007, 04:01 PM
  3. More Program Troubles...
    By face_master in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 05:40 AM
  4. Program Troubles...
    By face_master in forum Windows Programming
    Replies: 2
    Last Post: 07-31-2002, 05:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21