Thread: Having an output issue working with input file, golf game.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    20

    Having an output issue working with input file, golf game.

    My code is down below. I trying to get an output from this input file input.txt.
    This is a golf game and the first row is the number of golfers,
    second row is the par and the last two rows are the players scores.
    The breakdown is how the player scored in each hole.

    2
    3 4 4 4 4 5 5 3 3 4 4 4 4 4 5 3 3 4
    4 4 5 4 6 4 5 6 3 4 4 4 5 4 3 3 5 4
    2 3 4 4 4 5 5 3 3 4 4 4 4 4 4 3 3 4

    My output should be

    Player 1 scored 7.
    Breakdown: 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0

    Player 2 scored -3.
    Breakdown: -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0

    Player 2 is the winner!

    Im getting an output like this

    player 1 score -1
    Breakdown: 7290888

    player 2 score -2
    Breakdown: 7290888

    Can anyone help?? please?


    Code:
    #include <stdio.h>
    
    
    int main() {
    
    
      FILE *ifp;
      int golfer, counter, par[18], i, j, score=0, player[4][18], breakdown[18];
      //Opening input file.
      ifp = fopen("input.txt","r");
      //Getting amount of players.
      fscanf(ifp, "%d", &golfer);
        //Calculating the par.
        for(i = 0; i < 18; i++) {
                fscanf(ifp, "%d", &par[i]);
        }
    
    
        for(counter = 0; counter < golfer; counter++) {
    
    
            //Getting the players' scores.
            for(i = 0; i < golfer; i++){
                for(j = 0; j < 18; j++){
                    fscanf(ifp, "%d", &player[i][j]);
                }
            }
    
    
            for(i = 0; i < golfer; i++){
            //Calculating the breakdowns and score.
                for(j = 0; j < golfer; j++){
                breakdown[j] = player[i][j] - par[i];
    
    
                score += breakdown[j];
    
    
                }
            }
    
    
    
    
            //Printing the scores.
            printf("Player %d Score: %d.\n", counter+1, score);
            printf("Breakdown: %d.\n\n", breakdown[j]);
    
    
        }
    
    
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't print the entire array with this:
    Code:
    printf("Breakdown: %d.\n\n", breakdown[j]);
    You need to be looping through that array. As it is, J is past the end of your array at that point, so you're printing some value that you shouldn't be trying to access. What should have clued you in was the part where you ended up with a single value instead of a series of values.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    the second for(j = 0; ... has the wrong limit. should be 18 is golfer and par[i] should be par[j].

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Bloody hell are you all in the same class? I swear there are two more threads on this already.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Cot 3223

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    I sort of understand what cuzah means when he says i need to be looping through the array. I dont have much clue to how i actuallly do that.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's like turning pages in a book. The book is an array of pages in this analogy. You start with the first one, keep turning, until you get to the last. Each page has a number, its index, just like each array element has an index. So if I say page[30] it means I am referring to page 30. If I want to loop through them I do this:
    Code:
    for(int page = 0; page < TOTAL_NUM_PAGES; page++)
     // do something with book[page]
    I have noticed none of you guys doing this golf exercise are really in control of loops which indicates that your instructors are quite incompetent.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    ok so i changed several things and i now got this code.

    im getting an output like this:
    Player 1 score 7
    Player 2 score -3
    Breakdown: 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0
    Breakdown: 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0

    but i for organization issues how do i change it to print like this:
    Player 1 scored 7.
    Breakdown: 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0

    Player 2 scored -3.
    Breakdown: -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0

    Code:
    #include <stdio.h>
    
    
    int main() {
    
    
      FILE *ifp;
      int golfer, counter, par[18], i, j, score=0, player[4][18], breakdown[4][18];
      //Opening input file.
      ifp = fopen("input.txt","r");
      //Getting amount of players.
      fscanf(ifp, "%d", &golfer);
        //Calculating the par.
        for(i = 0; i < 18; i++) {
                fscanf(ifp, "%d", &par[i]);
        }
    
    
            //Getting the players' scores.
            for(i = 0; i < golfer; i++){
                for(j = 0; j < 18; j++){
                    fscanf(ifp, "%d", &player[i][j]);
                }
            }
            //Calculating the breakdowns and score.
            for(i = 0; i < golfer; i++){
                score=0;
                for(j = 0; j < 18; j++){
                    breakdown[i][j] = player[i][j] - par[j];
                    score += breakdown[i][j];
                }
                printf("Player %d Score %d.\n", i+1, score);
    
    
            }
            printf("Breakdown: ");
            for(i = 0; i < golfer; i++){
                for(j = 0; j < 18; j++){
                    printf("%d ", breakdown[i][j]);
                }
                printf("\n");
            }
            
    
    
    return 0;
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by douglas481 View Post
    ok so i changed several things and i now got this code.

    im getting an output like this:
    Player 1 score 7
    Player 2 score -3
    Breakdown: 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0
    Breakdown: 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0

    but i for organization issues how do i change it to print like this:
    Player 1 scored 7.
    Breakdown: 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0

    Player 2 scored -3.
    Breakdown: -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0
    Good question. How do you do that? Stop and think about what needs to change to make one output look like the other. What isn't where it's supposed to be? What do you need to do so it is like it's supposed to be?

    It doesn't do me or you any good if I just tell you what to do. You'll just be stuck the next time you need to stop and figure something out.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Nice work stealing the code from the other guy posting for help. If you guys are our future, we are all doomed. Now if you'll excuse me I need to go finish my doomsday shelter.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    I didnt steal the code from anyone, i only changed the things in my code that were said in this thread. I was able to finally write the whole code the way i wanted to thanks for everyones help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User-specified input file issue.
    By ajrey92 in forum C Programming
    Replies: 5
    Last Post: 01-10-2011, 05:16 PM
  2. input file issue
    By satty in forum C Programming
    Replies: 2
    Last Post: 09-10-2010, 01:59 AM
  3. C++ File Input Output VS C file Input Output
    By forumuser in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2009, 06:46 AM
  4. Creating cheat for a golf game
    By Livijn in forum Game Programming
    Replies: 4
    Last Post: 08-14-2009, 12:12 PM
  5. File output not working.
    By System_159 in forum C++ Programming
    Replies: 5
    Last Post: 08-31-2007, 10:05 AM

Tags for this Thread