Simple grading report

joshmo 0 Tallied Votes 507 Views Share

The code prompts the user for a student name and the marks then prints out the report with a letter grade..It also asks the user for the file name and stores the data in the file.

/*Generates a simple grading report for each student*/

#include <stdio.h>

#define LEN 25

#define S 5

struct stud_rec

{

	char name[LEN+1];

	float num_grade;

	char let_grade;

};

struct stud_rec records[S]; 

void letter_grade(double average, double points);

main()

{

	int n,b,i,rec;

	char fname[LEN+1];

	double points,average;

	FILE *fp;

	printf("Please Enter a file name to store the data: ");

	scanf ("%s", &fname);
	
	fp = fopen(fname, "w");

	printf("How many records do you want to enter?: ");

	scanf("%d", &rec);

	for(i=0;i<rec;i++)

	{

		printf("Enter the student name: ");

		scanf("%s", &records[i].name);

		fprintf(fp,"\n%s\n",records[i].name);

		n=0;

		for (b=0;b<S;b++)

		{
			
			n++;

			printf("Enter Grade %d: ",n);

			scanf("%f", &records[b].num_grade);

			fprintf(fp,"%.1f ", records[b].num_grade);

		}

		printf("\n");

		printf("My Grading Report\n");

		printf("Student  Grade1  Grade2  Grade3  Grade4  Grade5  Tpts  FinalGrade  LetterGrade\n");

		printf("--------------------------------------------------------------------------------\n");
	
		printf("%s", records[i].name);

		for (b=0;b<S;b++)

		{

			printf("\t  %.1f",records[b].num_grade);


		}
		
		points=0;
		
		for(b=0;b<S;b++)


		{
			
		
			points=points+records[b].num_grade;
		
		}

		average = (points/S);

		printf("\t %.1f",points);

		printf("\t %.1f", average);

		letter_grade(average,points);
		
		printf("\n");

		printf("--------------------------------------------------------------------------------\n");

		printf("Class Average: %.lf\n\n", average);
	
	}

	fclose(fp);

	return 0;

}

void letter_grade(double average, double points)

{

		
	if (average>=80)

	{

		printf("\t\tA");

	}

	if ((average>=70) && (average<80))

	{
			
		printf("\t\tB");

	}

	if ((average>=60)&&(average<70))

	{

		printf("\t\tC");

	}

	if ((average>=50)&&(average<60))

	{

		printf("\t\tD");

	}

	else if(average<50)

	{

		printf("\t\tF");

	}

}

Dani AI

Generated

Good start, — the program captures the idea, but a few structural and safety problems will make the output wrong or crash in real use. Main issues: the student struct holds only one num_grade (you need an array to store five grades per student), records is sized by S so asking for more students overruns the array, inner loops index records[b] instead of records[i], scanf calls are unsafe (and often use & on arrays), fopen is not checked, and the letter routine both prints and mixes if/else in a confusing way. Also the "Class Average" printed inside the per-student loop is just that student’s average, not the class average.

Fixes to consider (minimal, focused changes):

  • change the struct so each student carries all grades, a total/average, and a letter:

    #define NUM_GRADES 5
    
    struct stud_rec {
        char name[LEN+1];
        float grades[NUM_GRADES];
        float total;
        float average;
        char letter;
    };
  • make letter_grade return a char so it’s a pure function:

    char letter_grade(double avg) {
        if (avg >= 80.0) return 'A';
        else if (avg >= 70.0) return 'B';
        else if (avg >= 60.0) return 'C';
        else if (avg >= 50.0) return 'D';
        return 'F';
    }
  • input/output and averaging: read num_students, allocate or limit to a safe max, read each student into records[i], loop j from 0..NUM_GRADES-1 to fill records[i].grades[j], sum to records[i].total, compute records[i].average, and set records[i].letter = letter_grade(...). Check fopen for NULL and validate scanf/fgets results. Compute class average as sum of all students’ totals divided by (num_students * NUM_GRADES).

Extra tips: use size-limited scanf("%25s", name) or fgets to accept spaces in names; avoid main() old-style — use int main(void); validate all user input; if you meant to tag C++ (as noted), prefer std::string, std::vector<float> and std::ofstream for a cleaner rewrite.

hacker9801 49 Junior Poster

lol.... that's C not C++

joshmo 8 Posting Whiz in Training

ooops sorry ma bad...

MPELANE 0 Unverified User

This is a good work man, even though i don't know nothing about any other programming language except pseudocode.the reason is because im first year in nwu.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.