FILE reading in ANSI compiler

This is a discussion on FILE reading in ANSI compiler within the C Programming forums, part of the General Programming Boards category; I am supposed to create a program that reads from an input file of names and grade scores, calculate the ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Exclamation FILE reading in ANSI compiler

    I am supposed to create a program that reads from an input file of names and grade scores, calculate the average and assign a total grade:

    The input file looks like:

    Michael Jackson
    88 90 93 89 94

    Tony Gwynn
    78 93 92 84 80

    ...and so on, with a name on one line followed by scores on the next

    My program works perfectly as far as calculation goes, but I am supposed to get all the data to print out on one line. I get this when I execute:

    Michael Jackson
    Average = 90.8 Grade = A

    But I am supposed to have:

    Michael Jackson, Average = 90.8, Grade =A

    What am I doing wrong? My code so far:

    Code:
    {
    FILE *infile, *outfile;
    char text[81], ch_grade;
    double average;
    int lines, k, score [5], i_ave;
    infile = fopen("score.dat", "r");
    if(infile == NULL){
    printf("File Opening Error!\n");
    exit(1);
    }
    lines=0;
    while(fgets(text,81,infile)!=NULL){
    lines++;
    if (lines%2 !=0){
    printf("Student %s", text);
    }else {
    sscanf(text, "%d %d %d %d %d",
    score, score+1, score+2, score+3, score+4);
    for(k=0, average=0.0; k<5; k++)
    average += (double)score[k];
    average /= 5.0;
    i_ave = (int)(average + 0.5);
    if(i_ave >=85) ch_grade = 'A';
    else if(i_ave >= 75) ch_grade = 'B';
    else if(i_ave >= 60) ch_grade = 'C';
    else if(i_ave >= 40) ch_grade = 'D';
    else ch_grade = 'F';
    printf("Average =%5.1f;", average);
    printf(" Grade = %c.\n", ch_grade);
    }
    }
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    printf("Student &#37;s", text);
    Perhaps the newline is retained from the read.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    doesn't work

    Quote Originally Posted by Dave_Sinkula View Post
    Code:
    printf("Student %s", text);
    Perhaps the newline is retained from the read.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    That was one of the things I originally tried, but in the while loop since it ends on an odd line and I used "text" to read on each line, all that gets printed is "Student" and then a random grade from one of the lines

    such as:

    "Student 100" Average 90.8, Grade A

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Either make your text file not have the empty blank line, or adjust your code according to the format of the text file?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Quote Originally Posted by Dave_Sinkula View Post
    Either make your text file not have the empty blank line, or adjust your code according to the format of the text file?

    Yea I can't change the text file and I tried flipping the structure where the program scans the scores first and then the names in the else statement but it didn't work

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    line &#37; 3 == 1 could be the name line
    line % 3 == 2 could be the data line
    line % 3 == 0 could be the blank separator line
    ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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