Can someone please help me with my program? I am new to C programming.

This is a discussion on Can someone please help me with my program? I am new to C programming. within the C Programming forums, part of the General Programming Boards category; Hello, I was wondering if someone can help me with file input and output. I basically have golf.txt document that ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Can someone please help me with my program? I am new to C programming.

    Hello,
    I was wondering if someone can help me with file input and output.
    I basically have golf.txt document that contains data from 8 golfers' scores while playing a game of golf. I used the data in the golf.txt document to create a program that read values from the .txt document and calculates the total score of each golfer. However, now i need to take it a step further and create a program that compares each golfers score to the par score and outputs a value for hole by hole.


    The program should output 18 different values for each golfer, where each value will contain (the number of tries- par score= score for that hole) and there are 18 holes in a game of golf so it make sense


    Each letter represents each golfers score on each hole. (A for hole 1, B for hole 2..... R for hole 18)

    It should look something like this for each golfer:
    Golfer #1’s score: A B C D E F G.....R
    Golfer #2.................
    Golfer #3..................




    This should not be too hard because I have it all the data I need in my golf.txt file:

    8 // This is the number of golfers
    4 4 3 5 4 3 4 5 6 5 4 4 4 5 5 4 3 3 // these are the par scores
    4 4 3 6 7 4 4 5 4 5 3 5 3 5 7 5 3 2 // for here down is each of the eight golfers score
    4 5 3 4 6 5 5 6 3 5 4 6 3 3 5 4 5 3
    3 4 5 6 4 4 4 5 5 5 4 5 4 3 3 5 4 3
    2 5 5 6 7 6 5 5 4 6 4 5 6 7 5 5 5 5
    4 5 6 3 3 5 4 4 3 3 2 5 6 6 5 5 3 3
    2 5 3 5 5 5 6 5 4 3 4 5 5 4 4 6 5 5
    6 5 4 5 5 3 5 6 5 4 6 5 6 4 4 6 5 4
    4 4 5 5 3 3 6 5 4 3 3 5 6 5 3 6 5 4




    MY QUESTIONS:

    1) Would arrays be the best way to go about completing this program?
    2) Would I need a 1d or 2d array to complete this program?
    3) If so, I am completely new to arrays! can anyone help me understand how I would set it up? I know that the basic format is something like int numbers [rows][columns]
    4) How would I add/ subtract array's positions from each other (for example row 2 column 3 + row 2 column 4) ?


    Thank You,
    Matt H.
    Last edited by matthayzon89; 02-27-2010 at 10:49 PM.

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    1. Arrays would definitely be useful.
    2. I would use a 1d array for the par scores and either a 2d array or an array of structs for the golfer scores. Since you're new to this, I would go with the 2d array for golfer scores.
    3. I'd make it an int[# golfers][18]
    4. You would use a loop to iterate through each element and add the elements just like any other variable. For example, part of your program might look like this:

    Code:
    for (i=0; i<18; ++i)
    {
        score[n][i] = tries[n][i] - par[i];
    }
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thank you for the response,

    For some reason my array program is not even compiling correctly...
    I am not even sure if I am on the right track... Can someone help me out and take a look?

    This is what i got so far:

    Code:
    #include <stdio.h>
    
    
    int main() {
    
      FILE *ifp;
      int ttl_golfers, counter, totalscore, ok, j, i, z, n;
      int tries [9][18];
      int score [9][18];
      int parscore[18];
    
      ifp = fopen("golf.txt","rt"); 
      
      fscanf(ifp, "%d", &ttl_golfers);
      printf("\n%d golfers played on this golf course:\n", ttl_golfers);
    
      
      for (z=0; z < 18; z++)  {
        fscanf(ifp, "%d", &parscore);
        parscore[18] = parscore[z];
        }
                                             
       for(i = 0; i < 18; i++) {
        fscanf(ifp, "%d", &score);
        score[n][i] = tries[n][i]- parscore[i];
        }
       for(counter=0; counter < ttl_golfers; counter++) {
        }
    
      printf("\nGolfer #%d's Score: %d\n", counter, score-parscore);
      
    
    }
    
      fclose(ifp);
      
       
      system("PAUSE");
      return 0;
    
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    Code:
    fscanf(ifp, "%d", &parscore);
    you are missing [z] after parscore
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    This is my new code and it is still not compiling our outputting any values:/

    Any ideas?

    Code:
    #include <stdio.h>
    
    
    int main() {
    
      FILE *ifp;
      int ttl_golfers, counter, totalscore, ok, j, i, z, n, p, o;
      int tries [9][18];
      int score [9][18];
      int parscore[18];
    
      ifp = fopen("golf.txt","rt"); 
      
      fscanf(ifp, "%d", &ttl_golfers);
      printf("\n%d golfers played on this golf course:\n", ttl_golfers);
    
      
      for (z=0; z < 18; z++)  {
        fscanf(ifp, "%d", &parscore[z]);
        parscore[z] = parscore[z];
        }
                                             
       for(i = 0; i < 18; i++) {
        fscanf(ifp, "%d", &score[n][i]);
        score[n][i] = tries[n][i]- parscore[z];
        }
       for(counter=0; counter < ttl_golfers; counter++) {
        }
    
      printf("\nGolfer #%d's Score: %d\n", counter, score[n][i]-parscore[z]);
      
    
    }
    
      fclose(ifp);
      
       
      system("PAUSE");
      return 0;
    
    }

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    Code:
    for(i = 0; i < 18; i++) {
        fscanf(ifp, "%d", &score[n][i]);
        score[n][i] = tries[n][i]- parscore[z];
        }
    where n is initialized?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Hm... Im not sure what u meant by "Where n is initialized?" but I guess u mean that I forgot to include n=0? right? Sorry, im not very good with programming yet, im working on it

    This is my new code (which I am still not able to compile:

    Code:
    #include <stdio.h>
    
    
    int main() {
    
      FILE *ifp;
      int ttl_golfers, counter, totalscore, ok, j, i, z, n=0, p, o;
      int tries [9][18];
      int score [9][18];
      int parscore[18];
    
      ifp = fopen("golf.txt","rt"); 
      
      fscanf(ifp, "%d", &ttl_golfers);
      printf("\n%d golfers played on this golf course:\n", ttl_golfers);
    
      
      for (z=0; z < 18; z++)  {
        fscanf(ifp, "%d", &parscore[z]);
        parscore[z] = parscore[z];
        }
                                             
       for(i = 0; i < 18; i++) {
        fscanf(ifp, "%d", &score[n][i]);
        score[n][i] = tries[n][i]- parscore[z];
        }
       for(counter=0; counter < ttl_golfers; counter++) {
        }
    
      printf("\nGolfer #%d's Score: %d\n", counter, score[n][i]-parscore[z]);
      
    
    }
    
      fclose(ifp);
      
       
      system("PAUSE");
      return 0;
    
    }

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Code:
    }
    
      fclose(ifp);
      
       
      system("PAUSE");
      return 0;
    
    }
    this is in its own stament block and is all messed up, delete the topmost } in this code snippet above^^

  9. #9
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Code:
       for(i = 0; i < 18; i++) {
        fscanf(ifp, "%d", &score[n][i]);
        score[n][i] = tries[n][i]- parscore[z];
        }
       for(counter=0; counter < ttl_golfers; counter++) {
        }
    
      printf("\nGolfer #%d's Score: %d\n", counter, score[n][i]-parscore[z]);
    the bolded code above should be inside the for loop above (its an empty for loop) at this point if you fix that, it does in fact compile....however it segfaults immediately upon running, ill leave that up to you to find out why

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thank you! My program is finally compiling

    I am unsure what u meant by "the bolded code" because it did not show up in bold lettering. I might of misunderstood you:/

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    My program is now compiling correctly, however it is running completely wrong...

    Any ideas or advice? .... I think my logic for my loops are incorrect but it makes sense to me




    Code:
    #include <stdio.h>
    
    
    int main() {
    
      FILE *ifp;
      int ttl_golfers, counter, totalscore, ok, j, i, z, n=0, p, o;
      int tries [9][18];
      int score [9][18];
      int parscore[18];
    
      ifp = fopen("golf.txt","rt"); 
      
      fscanf(ifp, "%d", &ttl_golfers);
      printf("\n%d golfers played on this golf course:\n", ttl_golfers);
    
    
      for (z=0; z < 18; z++)  {
        fscanf(ifp, "%d", &parscore[z]);
        parscore[z] = parscore[z];
        }
    
    
     for(counter=0; counter < ttl_golfers; counter++) {
          for(i = 0; i < 18; i++) {
         for(n = 0; n < 8; n++) 
        fscanf(ifp, "%d", &tries[n][i]); 
        score[n][i] = tries[n][i]- parscore[z];
        printf("\nGolfer #%d's Score: %d\n", counter, tries[n][i]-parscore[z]);
    
        }
        }
    
    
      fclose(ifp);
      
       
      system("PAUSE");
      return 0;
    
    
    }
    Last edited by matthayzon89; 02-28-2010 at 04:00 PM.

  12. #12
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Code:
       for(counter=0; counter < ttl_golfers; counter++) {
        }
    
      printf("\nGolfer #%d's Score: %d\n", parscore[z]);
    As I said prior the printf above sould go into the for loop just above it, right now that for loop does absolutely nothing.

    Beyond that however your problems are mainly due to excessive looping and using an inferior method of reading in your records.

    Whenever reading data from a file (a flat text file, not binary) you should be using fgets and an appropriate buffer and you should verify the line was read in correctly before attempting to use it or split it up into scores/arrays

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Can you please view my "newer" code because I adjusted my code after the advice you gave me.

    Look at post #11 (two responses up)



    Thank you for your help

  14. #14
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Quote Originally Posted by nonpuz View Post
    Beyond that however your problems are mainly due to excessive looping and using an inferior method of reading in your records.

    Whenever reading data from a file (a flat text file, not binary) you should be using fgets and an appropriate buffer and you should verify the line was read in correctly before attempting to use it or split it up into scores/arrays
    refer to this ^^

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Oh okay...

    What exactly is a buffer?

    See I tried to make sure that my code is getting read in correctly but it is not, which is not surprising, but why is this? Even when it is as simple as reading in the first value in the document its reading it as 46 instead of 8

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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