You are here:

C++/C++ help with a problem

Advertisement


Question
Hi thank you so much for your input. I went online with the teacher today and went over the code remotely. I am still having issues with writing variables and then coming up with the total for labs, exams, and final grade. I am not sure if I wrote it correctly and if I did not for creating the variables I need help doing so as I have tried my best.

Here is what I have as to date since working with my professor.



#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();

  //calcLetterGrade();
     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);
{
 float Labs = (total / 8) * 0.40;  //not sure if this is correct
 float Exams= (total / 3) * 0.40;  //not sure if this is correct
 float Final= (total / 1) * 0.20;  //not sure if this is correct
 {
  float z =(Labs+Exams+Final)/3; //not sure??
  return z;    //not sure do I need this???
 

//add the total
// place the total in the mainline
//*ptr_PercentGrade = ??;
}
void calcLabs(float * ptr_Labs)
{
  int Labs;
  float Labs, avg, total = 0.0;
  /*Asks for up to 8 Labs */
  for (Labs = 0; Labs < 8; Labs++)
  {
    printf("\nWhat is the Next Student's Lab Score? ");
    scanf("%f", &Labs);
    total += Labs;
  }
  avg = (total / 8) * 0.40;
  *ptr_Labs = avg;
  
}

void calcExams(float * ptr_Exams)
{
  int Exams;
  float Exams, avg, total = 0.0;
  /*Asks for up to 3 Exams */
  for (Exams = 0; Exams < 3; Exams++)
  {printf("\nWhat is the Next Student's Exam Score? ");
  scanf("%f", &Exams);
  if (Exams < 0.0)
  {break;}
  total += Exams;
  }
  avg = (total / 3) * 0.40;
  *ptr_Exams = avg;
   

}
void calcFinal(float * ptr_Final)
{
   int Final;
  float Final, avg, total = 0.0;
  /*Asks for up to 1 Final */
  {printf("\nWhat is the Next Student's Final Score? ");
  scanf("%f", &Final);
  if (Final < 0.0)
  {break;} /*Quits early if no more students */
  total += Final;
  }
  avg = total / 1) * 0.20;
  *ptr_Final = avg;
    
}
void calcLetterGrade(float PercentGrade, char * ptr_LetterGrade)
{
  if (PercentGrade > 100.00)
  {
     *ptr_LetterGrade ='*';
  }

if (percentGrade > 94.0)
   {
    *ptr_LetterGrade ='A';
   }

 if ((PercentGrade > 85.0) && (PercentGrade < 93.0))
   {
    *ptr_LetterGrade ='B';
   }

 if ((PercentGrade > 75.0) && (PercentGrade < 84.0))
  {
   *ptr_LetterGrade ='C';
  }

   if ((PercentGrade > 65.0) && (PercentGrade < 74.0))
  {
   *ptr_LetterGrade ='D';
  }

  if ((PercentGrade < -10)
  {
   *ptr_LetterGrade ='*';
  }


//also are my open curly closed curly correct. I am not sure if I have them in the correct place.

//Thanks so much

Answer
Hello Shannon

I think you will have problems if you try to write the entire program at once and then try to compile it and then try to test it. It is not a good way for a beginner to work. The program you sent me does not even compile. As you are writing your program, get one small feature written, then compile and then test. In that way whenever you add a feature you will be adding to an already working program. Also, as a beginner, you should compile often. Write a function, compile it, write some test code to test it.

OK, lets look at the code. You had:

void calcPercentGrade(float * ptr_PercentGrade)
{
   float Labs = (total / 8) * 0.40;  //not sure if this is correct
   float Exams= (total / 3) * 0.40;  //not sure if this is correct
   float Final= (total / 1) * 0.20;  //not sure if this is correct

   float z =(Labs+Exams+Final)/3; //not sure??
   return z;    //not sure do I need this???


   //add the total
   // place the total in the mainline
   //*ptr_PercentGrade = ??;
}

Where is total comming from? It should be passed into the function. Generally functions return values through the return statement, not usually through passed in pointers. You don't need to pass in ptr_PercentGrade. Your arithmetic is not correct. You cannot find the grade with just 1 total.
The formula is
total of labs / 8 * .4
total of exams / 3 * .4
final exam * .2

When you return a value, like you are returning z above, you need to declare the function with a return type.

So using your program structure the calcPercentGrade looks like this

/*
Function calcPercentGrade. Takes the total of labs, exams, and the finalexam and
calculates the final mark. The function calculates the averages and applies the weights for the course.
*/
float calcPercentGrade(float totalLabs, float totalExams, float finalExam)
{
   float Labs = (totalLabs / 8) * 0.40;
   float Exams= (totalExams / 3) * 0.40;
   float Final= finalExam * 0.20;

   float z = Labs+Exams+Final;
   return z;
}

It could be made shorter, but I wanted to show you the steps.


Now lets look at calcLabs

void calcLabs(float * ptr_Labs)
{
   int Labs;
   float Labs, avg, total = 0.0;
   /*Asks for up to 8 Labs */
   for (Labs = 0; Labs < 8; Labs++)
   {
       printf("\nWhat is the Next Student's Lab Score? ");
       scanf("%f", &Labs);
       total += Labs;
   }
   avg = (total / 8) * 0.40;
   *ptr_Labs = avg;

}

This is OK, but you don't need to divide by 8 to get the average, and you don't need to apply the weight of 0.40 here because you are doing both in the calcPercentGrade.

When you write a function like calcLabs, you should write a comment above it describing what it is to do. Is it to calculate the total of the labs ? the average of the labs ? the average of the labs with the .4 weight applied ? You need to have it clear in your mind, in English, what the function is to do.

The same comments apply to calcExams and calcFinal.

calcLetterGrade seems OK.

Lets look at calcFinal in more detail.


void calcFinal(float * ptr_Final)
{
   int Final;
   float Final, avg, total = 0.0;
   /*Asks for up to 1 Final */
   {printf("\nWhat is the Next Student's Final Score? ");
   scanf("%f", &Final);
   if (Final < 0.0)
   {break;} /*Quits early if no more students */
   total += Final;
   }
   avg = total / 1) * 0.20;
   *ptr_Final = avg;

}

The program is supposed to quit when an empty student name is entered, not when Final < 0.0.
In fact, the break wont work here at all. The break can only be used in switch structures, and loops. You have neither here.  You can take that part out.

Here is all calcFinal needs to do.

/*
function calcFinal
Asks the user for the final mark, and returns it.
*/
float calcFinal()
{
   int Final;

   /*Asks for up to 1 Final */
   {printf("\nWhat is the Next Student's Final Score? ");
   scanf("%f", &Final);

   return Final;
}

Again, if the weighting will be done in calcPercentGrade, you don't need to do it in calcFinal.

I hope this has helped you a little. I suppose you have not done structures yet, so my StudentRecord structure must have made no sense to you. Good luck.

Best regards
Zlatko

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.