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

  1. #16
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You would want to use a structure like this:

    Code:
    #include <stdio.h>
    #define MAX_LINE BUFSIZ
    ....
    ....
    char buf[MAX_LINE+1];
    FILE * somefile = NULL;
    int players = 0;
    ...
    if (!(somefile = fopen("myfile.txt","r"))
      return 0;
    ...
    fgets(buf,MAX_LINE,somefile);
    if (!feof(somefile)) {
      sscanf(buf,"%d",&players);
      printf("number of players %d\n",players);
      while (fgets(buf,MAX_LINE,somefile) && !ferror(somefile)) {
        /* process buf here, ie convert it to a number */
        /* ie, read in each of the score lines, and then split them into the scores and do whatever */
      }
    }
    
    fclose(somefile);
    Point is you want to make sure you read in the entire line up to the \n (and including it) so the next call to fgets() starts exactly at the start of the next line that is the problem with fscanf, unless you do something like "%d\n" for your format string

    So even with fgets, if the line is longer then the size you pass it it will not read in the entire line and the next fgets call will start where the last one left off.
    Last edited by nonpuz; 02-28-2010 at 06:13 PM. Reason: put #define with BUFSIZ before include of stdio, mistake..

  2. #17
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I think it's fine for him to use scanf for now. I think he should focus on fixing his program logic before going for this kind of robustness.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #18
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thank you for the response, I was trying to understand how your code works but I am I think it is above my level, I think it is C+ or C++.... I am only falmiliar with C.

    I tried copying the code and editing my code into it but I was unable too...

    Thanks anyways though

  4. #19
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by NeonBlack View Post
    I think it's fine for him to use scanf for now. I think he should focus on fixing his program logic before going for this kind of robustness.


    I have been working on my code for hours now, do you have any suggestions about how to fix the logic? I tried so many different things I am running out of ideas.

  5. #20
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Is this a correct code for reading the first line from the golf.txt document? I know it is not, but I have no clue why I tried to mess around with it...


    golf.txt document:

    8
    3 3 4 5 5 4 3 3 3 4 4 5 4 3 3 5 4 3
    3 3 2 4 5 4 4 4 4 5 3 5 3 5 1 4 3 2
    4 5 3 4 4 3 5 3 3 3 4 2 3 3 4 4 5 3
    3 4 5 6 4 4 4 4 3 5 4 5 4 3 3 5 4 3
    2 4 5 5 5 5 5 3 3 5 4 4 2 4 4 5 5 4
    4 5 6 3 3 5 4 4 3 3 2 4 5 4 5 5 3 3
    2 5 3 5 5 5 3 4 3 4 4 4 3 4 4 5 5 5
    3 4 4 5 4 3 4 4 3 4 5 5 4 3 4 4 5 4
    4 2 4 3 5 3 3 4 5 2 3 3 5 5 5 3 5 5



    Code:
    for (z=0; z < 18; z++)  {
        fscanf(ifp, "%d", &parscore[z]);
        parscore[z] = parscore[z];
        }

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Delete the parscore[z] = parscore[z]. That tells you that you are now, you - rubbish.

    Nonpuz's idea of a struct is good, but overkill.

    I'll look at your code when I return from an errand - about an hour. You just want the "true" score by comparing the par score with the score they made, on each hole, correct?
    Last edited by Adak; 02-28-2010 at 07:30 PM.

  7. #22
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Here is your code, working and cleaned up, minus extra logic/variables etc:

    Code:
    #include <stdio.h>
    
    int main() {
        FILE *ifp;
        int ttl_golfers, totalscore, ok, j, i, z, n, p, o;
        int tries [8][18];
        int score [8][18];
        int parscore[18];
    
        ifp = fopen("golf.txt","r");
    
        fscanf(ifp, "%d\n", &ttl_golfers);
        printf("%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];
        }
        fscanf(ifp,"\n");
    
        for(n=0; n < ttl_golfers; n++) {
            for(i = 0; i < 18; i++) {
                fscanf(ifp, "%d", &tries[n][i]);
                score[n][i] = tries[n][i]- parscore[i];
                printf("Golfer #%d's Score: %d\n", n, score[n][i]);
            }
            fscanf(ifp,"\n");
        }
    
        fclose(ifp);
        return 0;
    }
    Please note that this code will ONLY work (and will likely crash) if it is not given a golf.txt file in its current directory that is PERFECTLY formatted, meaning exactly like this:

    Code:
    8
    4 4 3 5 4 3 4 5 6 5 4 4 4 5 5 4 3 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 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
    Note that after each of the lines with 18 digist (not the first line with only one digit) there is a single TRAILING space, this is REQUIRED for it be read correctly.

    The slightest mistake, such as 2 extra spaces at the end will result in a file that will not be parsed correctly with the code above.

    That is why I mentioned the fgets method as its much more flexible with how the file appears, you could put all kinds of stuff to ignroe comments, extra spacing, etc...with fscanf it is a pain

  8. #23
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by Adak View Post
    Delete the parscore[z] = parscore[z]. That tells you that you are now, you - rubbish.

    Nonpuz's idea of a struct is good, but overkill.

    I'll look at your code when I return from an errand - about an hour. You just want the "true" score by comparing the par score with the score they made, on each hole, correct?
    Right, so the program should output 18 different values that compare the par score to the actual score for each golfer. Thank you Adak.

  9. #24
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by nonpuz View Post
    Here is your code, working and cleaned up, minus extra logic/variables etc:


    Please note that this code will ONLY work (and will likely crash) if it is not given a golf.txt file in its current directory that is PERFECTLY formatted, meaning exactly like this:


    Note that after each of the lines with 18 digist (not the first line with only one digit) there is a single TRAILING space, this is REQUIRED for it be read correctly.

    The slightest mistake, such as 2 extra spaces at the end will result in a file that will not be parsed correctly with the code above.

    That is why I mentioned the fgets method as its much more flexible with how the file appears, you could put all kinds of stuff to ignroe comments, extra spacing, etc...with fscanf it is a pain
    THANK YOU~! I will try to look at it for a while to understand what I did wrong.... I would like to get a better understanding on how fgets works because I really don't like scanf. Ill look into when I get a chance... if you have any urls that you would recommend I would definitely read them

    Thank you for your help.

  10. #25
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    The code does not run correctly..I am trying to figure out why.... non-puz

  11. #26
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    is your golf.txt formatted exactly as I said? each line with 18 digits should have an EXTRA space at the END of the line, ie after the last digit

    and there should be no comments or anything in there...works for me

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Spaces should be no problem, using either fscanf() or sscanf(). When you fscanf() for a number, whitespace (spaces, newlines, etc.), are passed over, with no problem.

    Nonpuz - why keep the parscores[z] = parscores[z]; line?

    Matt, what kind of output do you want, (that is, how do you want it arranged?). I know what you want, but not how you want it to look. If you can show me, I'll match it up.
    Last edited by Adak; 02-28-2010 at 07:59 PM.

  13. #28
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Works great! thank you,
    it was my fault, I forgot to refresh my golf.txt document so it was getting the wrong values.


    The way it is outputting should be like GOLFER1: -1 2 5 4 3 3 5 4.........(18times for each golfer)

    but instead it is outputting like:

    Golfer1: -1
    Golfer1: 2
    Golfer 1:5
    ...
    ...
    Upto (18times for each golfer)


    Any ideas?

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sure - print the golfer number, then in a for loop, print the real scores, and leave off the newline between the scores.

    Just one newline between golfer numbers, only.

  15. #30
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    #include <stdio.h>
    
    int main() {
        FILE *ifp;
        int ttl_golfers, totalscore, ok, j, i, z, n, p, o;
        int tries [8][18];
        int score [8][18];
        int parscore[18];
    
        ifp = fopen("golf.txt","r"); 
    
        fscanf(ifp, "%d", &ttl_golfers);
        printf("%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(n=0; n < ttl_golfers; n++) {
            printf("Golfer #%d Scores:\n", n+1);
            printf("\t1\t2\t3\t4\t5\t6\t7\t8\t9\n\t");
            for(i = 0; i < 9; i++) {
                fscanf(ifp, "%d", &tries[n][i]); 
                score[n][i] = tries[n][i] - parscore[i];
                printf("%d\t", score[n][i]);
            }
            printf("\n\n\t10\t11\t12\t13\t14\t15\t16\t17\t18\n\t");
            for(i = 9; i < 18; i++) {
                fscanf(ifp, "%d", &tries[n][i]); 
                score[n][i] = tries[n][i] - parscore[i];
                printf("%d\t", score[n][i]);
            }
            printf("\n");
        }
    
        fclose(ifp);
        return 0;
    }
    Adak is right you dont need the \n stuff in the scanf, just use fscanf(%d and it will look over any whitespace/newlines...

    but you have to make sure there is no comments in there and no extra numbers or anything...

    the code above displays the results a little nicer and removed the excessive fscanf calls

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, 01: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, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM