Thread: C beginner. Having a really hard time with my program. Please help.

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

    Newbie question.

    Hello,

    This should be very simple but i just can't get it.

    I am working on a program dealing with file input/output. The program should basically read golf scores from a golf.txt document that exist, and add up each players score and output a sum.

    I have created a golf.txt document that is suppose store all the data for my program to read from.

    the golf.txt file looks something like this:

    2 // This is number of golf players
    4 4 3 5 4 3 4 5 6 5 4 4 4 5 5 4 3 3 //This is player#1 score for the game
    4 4 3 6 7 4 4 5 4 5 3 5 3 5 7 5 3 2 // This is player#2 score the game.

    NOTE: The only part where I get stuck, during this program is coming up with the code to add up each row of integers from the golf.txt document individually and output a sum in my program.

    -I have attached my program, so far, to this thread.


    Any help and advice will be greatly appreciated.

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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    if(fp=fopen("Golfscores.txt", "rt")) == NULL) {
      printf("\nError opening file = terminating.\n");
      return 1;
    }
    
    fscanf("%d", &numGolfers);
    
    for(i = 0; i < numGolfers; i++) {
      roundScore = 0; 
      for(j = 0; j < 18; j++) {
        ok = fscanf(fp, "%d", &score);
        if(ok == 0) {
          printf("\nError reading scores: exiting\n")
          return 2;
        }
        roundScore += score);
      }
      printf("Golfer #%d:   Score on Round: %d ", i, roundScore);
    }
    Note that the above is very rigid: no partial rounds are reported, etc. Also note that this code has not been run, so it may not be perfect.

    If you have any trouble with it that you can't handle, let me know.

    Fore!

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Adak THANK YOU BROTHER! your the man! I have been working on it all day long and I saw no progress.... thank you

    So, now it is better but still not where I need to be, instead of adding up each row individually it is adding up the first row and then adding it up to the total of the second row and then the first and second rows total is added up to the third rows total, etc....upto row 9.

    I need my program to sum up the total for the first row and output a value, then sum up the total for the second and output a value (each row represents one player's scores for a game of golf so adding two rows together would mean im adding two players' score up which is not what I need)

    So I am trying to figure it out again, and I have made some changes to my code considering the code you posted

    This is my new code, any idea?:

    Code:
    #include <stdio.h>
    
    int main() {
    
      FILE *ifp;
      int ttl_golfers, counter, score, totalscore=0, ok, j;
    
      ifp = fopen("golf.txt","r"); 
      
      
        fscanf(ifp, "%d", &ttl_golfers);
        printf("%d golfers played on this golf course:\n\n", ttl_golfers);
        
        for(counter = 1; counter < ttl_golfers; counter++) {
    
            for(j = 0; j < 18; j++) {
            fscanf(ifp, "%d", &score);
            totalscore += score;
          }
          printf("Golfer #%d's Score: %d\n\n", counter, totalscore);
          
    }
    
    
    
    system("PAUSE");
    return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Compare your code to my snippet. Mine has two nested for loops. Yours only has one.

    Fore!

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thank you, that was my fault, you had it right from the beginning!

    LOL okay, almost there....

    Now for my .txt document with all the golf scores which is pasted below, the number 8 represents the number of golfers and then the first row of scores represents the "par" scores and every line after that is the golfers scores which needs to compare to the par scores so I would have to actually calculate the sum of the first row under the 8, which is the par scores, separately and then subtract that from each persons totals to get the REAL final score of each golfer. In addition, the par score should not be calculated as any golfer's "total score". Which I am trying to figure out how to fix......with no success.


    Also, for some odd reason it is saying that the par score adds up to be 75 when it should be 68:/

    Accomplishing this should complete my two day long coding journey

    This is my golf.txt document:

    8
    3 3 4 5 5 4 3 3 3 4 4 5 4 3 3 5 4 3
    4 4 3 6 7 4 4 5 4 5 3 5 3 5 7 5 3 2
    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 4
    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 5 5 6 4 3 3 6 5 4 3 3 5 6 5 3 6 5


    This is my new code:

    Code:
     
    #include <stdio.h>
    #define parscore 68
    
    int main() {
    
      FILE *ifp;
      int ttl_golfers, counter, score, totalscore, ok, j;
    
      ifp = fopen("golf.txt","r"); 
      
      
        fscanf(ifp, "%d", &ttl_golfers);
        printf("%d golfers played on this golf course:\n\n", ttl_golfers);
        
        for(counter = 1; counter <= ttl_golfers; counter++) {
            totalscore= 0;
            for(j =0; j < 18; j++) {
            ok = fscanf(ifp, "%d", &score);
            
               totalscore += score;
               }
          
          printf("Golfer #%d's Score: %d\n\n", counter, totalscore);
          
    }
    system("PAUSE");
    return 0;
    Last edited by matthayzon89; 02-27-2010 at 07:36 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh sure! Move the tee off point backward 10 more yards!

    Par scores need to be summed up before you get into the outer for loop for the golfers.
    Code:
    for(i = 0, parscores = 0; i < 18; i++) {
      //fscanf( get your score like the other one )
      //parscores += score;
    
    }
    Be careful,, because you have a define on "parscore", so don't use that word, adding an s is alright, though.

    You want to get rid of the define is that it?

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    My program is currently printing out everything incorrectly. It is including the "par score" line from golf.txt (which is the first row under the 8) as a golf player, which is wrong. I need to calculate the par score separately and subtract it from each golf players total. But note that with the way my loop is set up it is including "par scores" row as golfplayer#1 and leaving the actual scores from golfplayer #8 out. You understand where my problem is now?

    More specifically:
    row 1 in my golf.txt document= par scores
    row 2-9 in my golf.txt document= golf player socres

    My loop is calculating rows 1-8 as golf player scores, when my program should list row2= golfplayer#1, row3= golfplayer#2, row4= golfplayer#3 etc.

    and then all I need to do from there is subtract row1 total (parscores) from each golf players total.

    Thank you for all yourr help btw
    Last edited by matthayzon89; 02-27-2010 at 08:32 PM.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Also,
    This my new code which includes what u just gave me and it is not really working :/

    Code:
    #include <stdio.h>
    
    
    int main() {
    
      FILE *ifp;
      int ttl_golfers, counter, score, parscores, totalscore, ok, j, i;
    
      ifp = fopen("golf.txt","r"); 
      
      
        fscanf(ifp, "%d", &ttl_golfers);
        printf("%d golfers played on this golf course:\n\n", ttl_golfers);
    
        for(i = 0, parscores = 0; i < 18; i++) {
        fscanf(ifp, "%d", &parscores);
        parscores += score;
    
    }
         
        for(counter = 1; counter <= ttl_golfers+1; counter++) {
            totalscore= 0;
            for(j=0; j < 18; j++) {
            ok = fscanf(ifp, "%d", &score);
            
               totalscore += score;
               }
          
          printf("Golfer #%d's Score: %d\n\n", counter, totalscore);
          
    }
    
    
    
    system("PAUSE");
    return 0;
    
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    not &parscores, but &score on the fscanf() for the par sum.

    If you indent your subordinate lines of code just a few spaces to the right, you'd see it right off.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    My program is currently printing out everything incorrectly. It is including the "par score" line from golf.txt (which is the first row under the 8) as a golf player, which is wrong. I need to calculate the par score separately and subtract it from each golf players total. But note that with the way my loop is set up it is including "par scores" row as golfplayer#1 and leaving the actual scores from golfplayer #8 out. You understand where my problem is now?

    More specifically:
    row 1 in my golf.txt document= par scores
    row 2-9 in my golf.txt document= golf player socres

    My loop is calculating rows 1-8 as golf player scores, when my program should list row2= golfplayer#1, row3= golfplayer#2, row4= golfplayer#3 etc.

    and then all I need to do from there is subtract row1 total (parscores) from each golf players total.

    Thank you for all yourr help btw

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Give me a few minutes and I'll post back with an edit to this post.



    Appears OK, but totally untested for accuracy.

    Code:
    #include <stdio.h>
    
    
    int main() {
    
      FILE *ifp;
      int ttl_golfers, counter, score, parscores, totalscore, ok, j, i;
    
      ifp = fopen("golf.txt","rt"); 
      
      
      fscanf(ifp, "%d", &ttl_golfers);
      printf("\n %d golfers played on this golf course:\n\n", ttl_golfers);
    
      for(i = 0, parscores = 0; i < 18; i++) {
        fscanf(ifp, "%d", &score);
        parscores += score;
      }
      printf("\n Par for this course is: %d", parscores);
         
      for(counter = 1; counter <= ttl_golfers; counter++) {
        totalscore= 0;
        for(j=0; j < 18; j++) {
          ok = fscanf(ifp, "%d", &score);
          totalscore += score;
        }
        printf("\nGolfer #%d's Score: %d\n", counter, totalscore);
      }
    
      fclose(ifp);
      //i = getchar(); ++i;
      system("PAUSE");
      return 0;
    
    }
    Last edited by Adak; 02-27-2010 at 09:06 PM.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    I really appreciate your help. Thank You!!!!!! It all works perfectly. THANK YOU!

    I hate to ask you this, but do you have any idea why the sum for the par scores is 75 instead of 68? I thought maybe it is adding the "number of golfers" value which is 8 to the sum of row 1 but it does not seem like its the case since 8+68=76


    Any ideas?

    Other than that my program is awesome and works perfectly, thanks again.


    This is my txt document:
    8
    3 3 4 5 5 4 3 3 3 4 4 5 4 3 3 5 4 3
    4 4 3 6 7 4 4 5 4 5 3 5 3 5 7 5 3 2
    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 4
    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 5 5 6 4 3 3 6 5 4 3 3 5 6 5 3 6 5

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Adak, NEVER MIND I figured it out, everything works perfect, i had a little error because I had to refresh my .txt document and C program for it to update the new values.


    Thanks again!
    Take care

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. little program to get time from log
    By repu1sion in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 10:05 AM
  2. How to time how long a program takes to run?
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 08-18-2008, 07:50 PM
  3. How to restrict a program in time consumed
    By gogoc in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2008, 11:10 AM
  4. Finding Program Running Time
    By pf732k3 in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 01:56 PM
  5. Replies: 11
    Last Post: 10-24-2004, 10:28 AM