You are here:

C++/C++ help with a problem

Advertisement


Question
Hi I have tried completing this and I having issues with it. I would appreciate any help you can provide me to complete this. Thank you so much!

Assignment: Compute a student's percentage grade and then assign the appropriate letter grade. Perform the computations  for up to 30 students and produce a report ( to a file ) in the format shown in the sample below

   INPUT:

   Student Name
   8 labs
   3 exams
   1 final

   COMPUTATIONS:

   The percentage grade is equal to the sum of:

   -- the average of 8 labs with a 40% weight
   -- the average of 3 exams with a 40% weight
   -- the final exam with a 20% weight

   LOGIC:
   94-100 A
   85-93 B
   75-84 C
   65-74 D
   BELOW 65 F

   OUTPUT:

   GRADE REPORT

   NAME % GRADE LETTER GRADE

   Jim Dandy 93 B
   Sally Sue 78 C
   Handay Andy 59 F

This is as far as I have gotten and now I am stuck, please help, thanks!

   #include<stdio.h>
   #include<stdio.h>


   void calcPercentGrade(float *);
   void calcLabs (float *);
   void calcExams (float *);
   void calcFinal(float *);
   void calcLetterGrade(float, char *);

   #define MSG_PAUSE(x) printf("\n%s",x);fflush(stdin); getchar()
   #define MAX_STUDENT 30
   main()
   {

   float PercentGrade =100.00f;
   char LetterGrade ='A';
   char sname[20];
   int scount =0;
   FILE * fptr;

   fptr = fopen("gradereport.txt", "w");
   if( fptr == NULL )
   {
   printf("\nError Opening File !");
   printf("\nPress any key to exit program.");
   fflush(stdin); getchar();
   exit(0);
   }
   fprintf(fptr, "\n G R A D E R E P O R T\n\n ");
   fprintf(fptr, "Name %% Grade Letter Grade\n\n ");


   printf("\nEnter students name or (0) to exit: (room for %d more) ",MAX_STUDENT- scount);
   fflush(stdin);
   scanf("%19[^\n]s", sname);

   while(sname[0] != '0')
   {

   //process
   printf("processing...");

   calcPercentGrade(&PercentGrade);


   calcLetterGrade(PercentGrade, &LetterGrade);
   scount++;
   fprintf(fptr, "\n%2d. %-12s%8.2f%5c",scount, sname, PercentGrade, LetterGrade);

   if(scount == MAX_STUDENT)
   {

   printf("\nThe class is full!");
   break;
   }

   printf("\nEnter students name or (0) to exit: (room for %d more) ",MAX_STUDENT- scount);
   fflush(stdin);
   scanf("%19[^\n]s", sname);

   }
   fprintf(fptr,"\n\nStudent Count %d", scount);


   fclose(fptr);


   return(0);
   }


   void calcPercentGrade(float * ptr_PercentGrade);
   {
   // create 3 vars

   calcLabs (&Labs);
   calcExams (&Exams);
   calcFinal (&Final);
   // add the total
   // place the total in the mainline
   *ptr_PercentGrade = ??;
   }

   void calcLabs(float * ptr_Labs)
   {

   }


   void calcExams(float * ptr_Exams)
   {


   }

   void calcFinal(float * ptr_Final)
   {

   }
   void calcLetterGrade(float PercentGrade, char * ptr_LetterGrade)
   {

   }

Answer
Hello Shannon

You have some good ideas in your program. I see that you are trying to split the program into functions and each function is going to have a single responsibility. That's good. What you need to do is to start small, and build on small successes. You are trying to create the entire program at once. Start with just one aspect, get it working, then add more.

When writing a program that takes input, I like to get my input from an array instead of the keyboard because while testing, it is really tedious to keep inputting from the keyboard. Input is the first part you should work on because without data, you cannot run your program. Have a look at how I did it below. Make sure you read the comments.

I have started you off with the program. I've organized the data into an StudentRecord which you will have to pass around to different functions. My idea was to process the labs, exams, and final in one function, but you can separate it out into 3 if you like.

Finish the program calculations, and the letter grade function, and do your output to the screen. Once you get that working, then add the output to the file. Since the destination of your output will change, it is good to separate your output code into its own function, just like I did for the input code.

Good luck. Let me know if you have more questions.

Best regards
Zlatko

#include<stdio.h>
#include<stdlib.h>

/*
You need to define a record which will hold your student data
You will do calculations on the student data
*/
#define MAX_SNAME 20
#define MAX_LABS 8
#define MAX_EXAMS 3
struct StudentRecord
{
   char sname[MAX_SNAME];
   float labs[MAX_LABS];
   float exam[MAX_EXAMS];
   float finalExam;
};

/*
For testing, it is easiest to have all the data in memory, in an array.
Once your program is working, you can take input from a file, the keyboard,
or whatever you like
*/
StudentRecord testInput[] =
{
   { "student1", {10,20,30,40,50,60,70,80}, {10,20,30}, 10},
   { "student2", {11,21,31,41,51,61,71,81}, {11,21,31}, 11},
   { "student3", {13,23,33,43,53,63,73,83}, {13,23,33}, 13},
};

/*
Below is a handy trick to find the size of an array
*/
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))

/*
The source of the student data will change as your program evolves.
At first it is from an array in memory. Later it might be from the keyboard.
Take what changes (in this case the source of the data) and wrap it in a function.
Later you can change the function as the input source changes.

inputStudent fills up the studentRecord passed into it. It returns true if
there was input, and false otherwise.
*/
bool inputStudent(StudentRecord& studentRecord)
{
   static int studentIndex = 0;
   if (studentIndex < ARRAY_SIZE(testInput))
   {
       studentRecord = testInput[studentIndex];
       ++studentIndex;
       return true; /* There was a student to process, return true */
   }

   return false; /* there was no input, return false */
}

float calcPercentGrade(StudentRecord& record)
{
   /* First do the labs */
   float labMark = 0; /* This will hold the totals, then the average */
   for(int labIndex = 0; labIndex < MAX_LABS; ++labIndex)
   {
       labMark += record.labs[labIndex]; /* total up the labs */
   }
   labMark /= MAX_LABS; /* find the average */
   labMark *= (float)0.40; /* weigh it at 40 percent */

   /* Now do the exams */
   float examMark = 0;

   /* Now do the final */
   float finalExamMark = 0;

   /* Now calculate and return the final mark */
   return labMark + examMark + finalExamMark;
}

/*
I like to put my main last, its just my habit, you do what you like.
*/
int main()
{
   StudentRecord student;

   while(inputStudent(student))
   {
       float percentGrade = calcPercentGrade(student);
       /* Since you are doing C++, not C, it would be better to use
       iostream input and output instead of stdio, but you do what you like
       printf and scanf are stdio (C)
       cin and cout are iostream (C++)
       */
       printf("Student %s grade %f\n", student.sname, percentGrade);

       /* TODO calculate the letter grade */
       /* TODO output results to the screen */
       /* TODO once everything is working, add file output */
   }
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.