Thread: A program to calculate students grades from an input file and write to output file

  1. #1
    Registered User
    Join Date
    Aug 2014

    Red face A program to calculate students grades from an input file and write to output file


    One of my class assignments is to create a program that receive a .txt file containing a students name and their grades as follows:

    John K. 99, 87, 57, 89, 90, 95
    Amanda B. Jones 100, 88, 76, 99, 86, 92

    The number of students is unknown until run time. You have to take those grades and average them weighing the first (4) at 10% a piece and the last (2) at 30% each.

    Then return an output file with the students name and their letter grade A,B,C,D,F based on their computed score. In addition, on screen it needs to display the average scores for each Q1, Q2, etc. as well as the minimum and maximum for each test on the screen.

    I am having a hard time in assigning the scores to a variable so that they can then be computed as an average and then used to determine a letter grade. I have begun to write the code and am a bit's what I have so far:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
        char fname[30];
        char minitial[1];
        char lname[100];
        int q1, q2, q3, q4, mid, final;
    } Student;
    int main()
        const int STUDENTSMAX = 100;
        Student students[STUDENTSMAX];
        int i = 0;
        char fname[30];
        FILE *ifp, *ofp;
    printf("Enter input .txt file name\n");
        ifp = fopen(fname,"r");
        ofp = fopen("output.txt", "w");
        if(ifp == NULL)
    printf("Error while opening the file.\n");
        if (ofp == NULL)
    printf("An error has been generated while attempting to open the output file");
            fscanf(ifp, "%s, %s, %d, %d, %d, %d, %d, %d", students[i].fname, students[i].lname, &students[i].q1, &students[i].q2, &students[i].q3, &students[i].q4, &students[i].mid, &students[i].final);
            printf("%s %s %d, %d, %d, %d, %d, %d\n\n", students[i].fname,students[i].lname,students[i].q1,students[i].q2,students[i].q3,students[i].q4,students[i].mid,students[i].final);
    Any sort of advice or direction would be helpful. Thank you!!!
    Last edited by Salem; 08-29-2014 at 10:38 PM. Reason: removed excess tags from code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > John K. 99, 87, 57, 89, 90, 95
    > Amanda B. Jones 100, 88, 76, 99, 86, 92
    How do you intend to cope with either the middle initial and/or the last name being optional?

    The first step would be to write down some rules for your file format. You'll need this to be able to tell where a name ends, and where marks begin.

    Also note that %s stops at the first white space, so you can't use it to read a name like "John K.".
    Hint: look up %[

    > while(!feof(ifp))
    FAQ > Why it's bad to use feof() to control a loop -
    Use fgets() to read a whole line into a buffer, then use sscanf() to extract information.
    One particular advantage of this approach is you can have several sscanf() attempts at parsing your data, if need be.
    Make sure to pay attention to the return result of sscanf() - it tells you how successful the scan was.

    > char minitial[1];
    If you intend storing a string like "K\0", then you need at least 2 chars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2012
    One other thing to note, you say in the statement of the problem that "the number of students is unknown", and yet you have placed an arbitrary limit of 100 for your "students" array. That is may be an issue.

    If you intend to put an arbitrary number of students into an internal array, then the array should be dynamically allocated (i.e. malloc/calloc & realloc) or converted to a list with each node dynamically allocated (i.e. same as above).

    If you don't dynamically allocate your array, then the size you choose for your array must be guaranteed to encompass the maximum students possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-12-2012, 10:21 PM
  2. Replies: 5
    Last Post: 04-25-2011, 01:12 PM
  3. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  4. Hopelessly Lost (calling file, write to value, calculate)
    By DJPlayer in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2007, 12:36 PM

Tags for this Thread