C++ Program to Read 10 Grades From a File and Display the Average
08-29-2014 #i
Registered User
A program to calculate students grades from an input file and write to output file
Hi,One of my class assignments is to create a plan that receive a .txt file containing a students name and their grades as follows:
John Grand. 99, 87, 57, 89, 90, 95
Amanda B. Jones 100, 88, 76, 99, 86, 92
etc..The number of students is unknown until run fourth dimension. You have to have those grades and average them weighing the offset (4) at ten% a slice and the concluding (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 improver, 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 difficult 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'southward what I have and so far:
Any sort of communication or direction would be helpful. Cheers!!!Code:
#include <stdio.h> #include <stdlib.h> #include <math.h> typedefstruct { char fname[xxx]; char minitial[1]; char lname[100]; int q1, q2, q3, q4, mid, terminal; } Student; int principal() { const int STUDENTSMAX = 100; Student students[STUDENTSMAX]; int i = 0; char fname[30]; FILE *ifp, *ofp; printf("Enter input .txt file proper noun\n"); scanf("%s",fname); strcat(fname,".txt"); ifp = fopen(fname,"r"); ofp = fopen("output.txt", "w"); if(ifp == NULL) { printf("Mistake while opening the file.\north"); exit(EXIT_FAILURE); } if (ofp == NULL) { printf("An error has been generated while attempting to open the output file"); fclose(ofp); leave(1); } while(!feof(ifp)) { fscanf(ifp, "%s, %due south, %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 %southward %d, %d, %d, %d, %d, %d\due north\n", students[i].fname,students[i].lname,students[i].q1,students[i].q2,students[i].q3,students[i].q4,students[i].mid,students[i].terminal); } printf("\north"); printf("\n"); fclose(ifp); }
Last edited by Salem; 08-29-2014 at 10:38 PM. Reason: removed excess tags from code
08-30-2014 #two
and the hat of int overfl
> 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 center initial and/or the concluding proper name being optional?The starting time step would be to write downwards some rules for your file format. Y'all'll need this to be able to tell where a proper noun ends, and where marks begin.
Likewise note that %s stops at the first white space, so you can't utilise information technology to read a proper name similar "John One thousand.".
Hint: expect upwardly %[> while(!feof(ifp))
FAQ > Why information technology's bad to use feof() to control a loop - Cprogramming.com
Utilise fgets() to read a whole line into a buffer, then use sscanf() to extract data.
One particular advantage of this arroyo is you tin can take several sscanf() attempts at parsing your information, if need be.
Make certain to pay attention to the return issue of sscanf() - information technology tells yous how successful the scan was.> char minitial[one];
If you lot intend storing a cord like "K\0", and then you need at least 2 chars.
Source: https://cboard.cprogramming.com/c-programming/164120-program-calculate-students-grades-input-file-write-output-file.html
Post a Comment for "C++ Program to Read 10 Grades From a File and Display the Average"