Computing Grades from input File and writing to output file

This is a discussion on Computing Grades from input File and writing to output file within the C Programming forums, part of the General Programming Boards category; Hi, One of my class assignments is to create a program that receive a .txt file containing a students name ...

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    3

    Computing Grades from input File and writing to output file

    Hi,

    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
    etc..

    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 next (2) at 15% each and the final at 30%.

    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 stuck..here's what I have so far:

    Code:
     
    Code:
    //
    //  main.c
    //  Final Exam
    //
    //  Created by Matt Spadafora on 8/28/14.
    //  Copyright (c) 2014 Matt Spadafora. All rights reserved.
    //
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int computeGrade();
    
    
    int main()
    {
    
     char fName[20];
     char lName[20];
     char buf[100];
     char *token;
     int q1, q2, q3, q4, mid1, mid2, final,total;
    
    
     char fname[30];
    
     FILE *ifp, *ofp;
    printf("Enter input .txt file name\n");
     scanf("%s",fname);
     strcat(fname,".txt");
        ifp = fopen(fname,"r");
        ofp = fopen("output.txt", "w");
    
    
     if(ifp == NULL)
        {
    printf("Error while opening the file.\n");
    exit(EXIT_FAILURE);
        }
     if (ofp == NULL)
            {
    printf("An error has been generated while attempting to open the output file");
    
     fclose(ofp);
     exit(1);
            }
    
    
     while (fgets(buf, sizeof(buf), ifp) != NULL)
    	{
    		token = strtok(buf, " ");
     strcpy(fName, token);
    		token = strtok(NULL, ",");
     strcpy(lName, token);
    		token = strtok(NULL, ",");
    		q1 = atoi(token);
    		token = strtok(NULL, ",");
    		q2 = atoi(token);
    		token = strtok(NULL, ",");
    		q3 = atoi(token);
    		token = strtok(NULL, ",");
    		q4 = atoi(token);
    		token = strtok(NULL, ",");
    		mid1 = atoi(token);
            token = strtok(NULL, ",");
            mid2 = atoi(token);
    		token = strtok(NULL, ",");
    		final = atoi(token);
     printf("%s %s, %d, %d, %d, %d, %d, %d, %d\n", fName,lName,q1,q2,q3,q4,mid1,mid2,final);
    	}
    
     computeGrade(&q1,&q2,&q3,&q4,&mid1,&mid2,&final);
    
     printf("%d", total);
    
     return 0;
    }
    
    
    int computeGrade(int *qA, int *qB, int *qC, int *qD, int *qE, int *qF, int *qG)
    {
     int total;
    
    printf("\n\nTHIS IS INSIDE THE PROGRAM!!!!\n");
     printf("%d, %d, %d, %d, %d, %d, %d\n",*qA, *qB, *qC, *qD, *qE, *qF, *qG);
    
        total += *qA, *qB, *qC, *qD, *qE, *qF, *qG;
     printf("\n%d", total);
     return total;
    }
    The problem I'm having now is how to go about passing the grades to the function computeGrade and then compute the average and return that to the function.

    Any help would be greatly appreciated..thanks!

    Last edited by new2coding; 08-31-2014 at 04:30 PM.

  2. #2
    Registered User
    Join Date
    Aug 2014
    Posts
    3
    This is the output I'm getting:

    Enter input .txt file name
    Graded
    Thui Bhu, 100, 90, 80, 100, 89, 99, 88
    Ariana B. Smith, 90, 90, 100, 100, 99, 100, 95
    Emily Gonzales, 100, 90, 100, 70, 78, 78, 80
    Jennifer L, 80, 90, 90, 100, 89, 99, 85
    Maria Jones, 65, 72, 77, 68, 62, 70, 65
    Bill Gates, 60, 54, 38, 62, 65, 60, 50
    Escobar Morris, 83, 77, 88, 76, 79, 72, 76
    Anne Latner, 80, 80, 85, 95, 90, 95, 90




    THIS IS INSIDE THE PROGRAM!!!!
    80, 80, 85, 95, 90, 95, 90


    328470Program ended with exit code: 0

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by new2coding View Post

    The problem I'm having now is how to go about passing the grades to the function computeGrade and then compute the average and return that to the function.

    Any help would be greatly appreciated..thanks!

    Right now you are returning the total from that function. Is your question how to compute the average from the total?? Btw you have some issues in posting your code.

    1. try to format it in plain text and proper indentation
    2. try to limit to the part relevant to your question. if you want us to complete your final exam we probably don't need to know this

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > return total;
    Where does this go, when the function returns to main?

    > ifp = fopen(fname,"r");
    Why do you have = here?
    Why doesn't your call to computeGrade have the same?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    zub
    zub is offline
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Split program to little parts and test each of them independently - C Testing for Embedded Applications with greatest

    For example, this is simple function, that fills array with numbers, extracted from string.

    Code:
    int* extract(char* str){
        static int arr[7];
        char* part = strtok(str, ",");
        int i;
        for( i = 0; i < 6; ++i ) {
            if( !part ) { break; }
            arr[i] = atoi(part);
            part = strtok(NULL, ",");
        }
        arr[i] = -1;
        return arr;
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-31-2014, 12:07 AM
  2. Naming an output file based on an input file name
    By mbxs3 in forum C Programming
    Replies: 4
    Last Post: 09-07-2013, 07:30 PM
  3. Reading from file to an array and computing the grades
    By acmarshall in forum C++ Programming
    Replies: 4
    Last Post: 12-24-2011, 07:42 PM
  4. Need Help With Basic Program (computing grades)
    By jsokol7 in forum C++ Programming
    Replies: 18
    Last Post: 06-21-2011, 12:15 PM
  5. Input-Output File--Can't create a file...
    By zaracattle in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2006, 11:15 AM

Tags for this Thread


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