Help with C++ Problem

May 4, 2010 at 16:27:48
Specs: Windows XP
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)
{

}


See More: Help with C++ Problem

Report •


#1
May 5, 2010 at 15:10:50
Okay, congrats on copy/pasting the framework source code your professor gave you. His style sucks. Now where are you stuck?

Report •

#2
May 5, 2010 at 17:04:34
I actually worked some more on the code and I got a bit further. I just need to write in variables and figure out the variables and //add the total
// place the total in the mainline
//*ptr_PercentGrade = ??;

****I am not sure if I wrote in the variables correctly for labs, exam, and final

void calcPercentGrade(float * ptr_PercentGrade);
{
float Labs = (total / 8) * 0.40;
float Exams= (total / 3) * 0.40;
float Final= (total / 1) * 0.20;

//printf("%f %f %f, Labs, Exams, Final");
//add the total
// place the total in the mainline
//*ptr_PercentGrade = ??;


*****This is what I have now for my code:

#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;
float Exams= (total / 3) * 0.40;
float Final= (total / 1) * 0.20;

//printf("%f %f %f, Labs, Exams, Final");


//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;} /*Quits early if no more students */
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 ='*';
}


Report •

#3
May 6, 2010 at 09:49:51
A few pitfalls you should be aware of; first and foremost, NEVER use fflush(stdin) as its behavior is undefined for input streams. It may do nothing, it may appear to work, or it just may thrash some point in memory.

Similarly, scanf is a risky function to use... especially with numeric input. If you wish to use or must use scanf, then make sure to check its return value before trusting its data. If it succeeds, it returns the number of items converted.

int num;

printf("Enter a number: ");
if((scanf("%d", &num)) != 1) {
   // input stream in error state
}

fgets is a much safer function to use for all input though

#include <string.h>

char input[80] = { 0 }, *p;

// for strings

fgets(input, sizeof input, stdin);

if((p = strchr(input, '\n')) != NULL) {
   *p = '\0'; // remove newline from string
} else {
   // keyboard buffer overflow
   // eat keyboard buffer until newline
   while(getchar() != '\n') {} 
}

// for numerical input

do {
   printf("Enter a number: ");
   fgets(input, sizeof input, stdin);
} while((sscanf(input, "%_data_type", &_variable_name)) != 1);


For your program, you'd probably have an easier time if you used structured data; however, if you wish
to write the file on the fly, then perhaps try something like.


#define MAX_LAB  8
#define MAX_EXAM 3
#define MAX_NAME 20

void getStudent(char *name, int *avg, char *grade) {

   char input[30] = { 0 }, *p;

   printf("Enter students name (0 to quit): ");
   fgets(input, sizeof input, stdin);
   if((p = strchr(input, '\n')) != NULL) { *p = '\0'; }

   if(strlen(input) == 0 || input[0] == '0') { 
      strcpy(name, "!");
   } else {

      int j, sum[2] = { 0 }, tmp;

      strncpy(name, input, MAX_NAME - 1);
 
      for(j=0; j < MAX_LAB; j++) {
         do {
            printf("Enter lab %d for %s: ", j + 1, name);
            fgets(input, sizeof input, stdin);
         } while((sscanf(input, "%d", &tmp)) != 1);

         sum[0] += tmp;
      }

      for(j=0; j < MAX_EXAM; j++) {
         do {
            printf("Enter exam %d for %s: ", j + 1, name);
            fgets(input, sizeof input, stdin);
         } while((sscanf(input, "%d", &tmp)) != 1);

         sum[1] += tmp;
      }

      do {
         printf("Enter final for %s: ", name);
         fgets(input, sizeof input, stdin);
      } while((sscanf(input, "%d", &tmp)) != 1);

      *avg = (int)(((sum[0] / 8) * 0.4) +
                   ((sum[1] / 3) * 0.4) +
                    (tmp * 0.2));

      *grade = (*avg >= 94 ? 'A' :
                *avg >= 85 ? 'B' :
                *avg >= 75 ? 'C' :
                *avg >= 65 ? 'D' : '*');
   }
}


int main(void) {

   char student_name[MAX_NAME] = { 0 }, letter;
   int avg;
   FILE *fp = fopen("test.txt", "w");

   if(fp != NULL) {

      getStudent(student_name, &avg, &letter);

      if(student_name[0] == '!') {
         puts("Class full");
      } else {
         fprintf(fp, "%s - %d - %c\n\n", student_name, avg, letter);
      }

      fclose(fp);
   }

   return 0;
}

Not sure what you're wanting to do within the functions with the pointers and such. Hopefully, the above will help you out or at least give you an idea or two.

Good luck.


Report •

Related Solutions

#4
May 6, 2010 at 15:14:12
Thank you. The second lab with start next week building off of this one using structs and strings. May need more help once I have tried to hash it out for myself a few times. Again, I appreciate your time and help!

Report •

#5
May 18, 2010 at 01:00:23

Hi I am back with the second go round with this problem-this time including structs and strings. I have done what I can with the info/assignment and now I am stuck. I appreciate your time and help!Thanks

DIRECTIONS:

OBJECTIVE: 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. (THIS WAS LAB 2 ASSIGNMENT. LAB THREE CONTINUES BELOW
This time use an array of structs to hold the student data. Add features to allow reports to be generated to the screen or file that sort by Name or by Percent Grade

-INCLUDE ABOVE MAIN:

Struct student
Char FirstName[15];
char LastName[20];
float PercentGrade;
char LetterGrade;

-Make sure to have the name broke into two name fields first, and last.

-typedef struct student STUDENT

-use case statements

-create menu such as:

Grade Machine

1. Enter Students
2. Display By Name to Screen
3. Display By GPA to Screen
4. Write By Name to File
5. Write By GPA to File
0. Exit

-Call in pairs, 1 to call, and 1 to reorder

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 WHAT I HAVE SO FAR****

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

typedef struct STUDENT
{
char FirstName[15];
char LastName[20];
float PercentGrade;
char LetterGrade;
}STUDENT;

//typedef struct student STUDENT;

#define MAX 7

void writeStudents(void);
void displayArray(STUDENT []);
void sortStudentsByFirstName(STUDENT []);
void sortStudentsByLastName(STUDENT []);
void sortStudentsByPercentGrade(STUDENT []);
void sortStudentsByLetterGrade(STUDENT []);

void enterStudents(void);

main()
{
int choice;

do
{
printf("\n\n\n Enter Students or 0 to exit\n\n");
printf("\n 1. Display By Name To Screen");
printf("\n 2. Display By GPA To Screen");
printf("\n 3. Write By Name To File");
printf("\n 4. Write By GPA To File");
printf("\n\n Enter you selection ==> :");
fflush(stdin);
scanf("%d", &choice);

switch(choice)
{

case 1 : printf("\nDisplay By Name");
break;
case 2 : printf("\nDisplay By GPA");
break;
case 3 : printf("\nWrite By Name");
break;
case 4 : printf("\nWrite By GPA");
break;
case 0 : break;
default: printf("\nYour options are 0-4");
printf("\nPress <Enter> to try again");
fflush(stdin);
getchar();
}
} while(choice);

return(0);
}
void enterStudents(void)
{
printf("\nDisplay By Name");
printf("\nDisplay By GPA");
printf("\nWrite By Name");
printf("\nWrite By GPA");
}

STUDENT mixed[15];
int i;
FILE * fptr;
writeStudents();
fptr = fopen("Students.dat", "rb");
if(fptr ==NULL)
{
printf("\n Grade Report Today!");
fflush(stdin);getchar();
exit(0);
}
i=0;

fread(&mixed[i],sizeof(STUDENT), 1, fptr);
while(!feof(fptr))
{
printf("\n %s", mixed[i].FirstName);
i++;
fread(&mixed[i], sizeof(STUDENT), 1, fptr);

printf("\n %s", mixed[i].LastName);
i++;
fread(&mixed[i], sizeof(STUDENT), 1, fptr);

printf("\n %s", mixed[i].PercentGrade);
i++;
fread(&mixed[i], sizeof(STUDENT), 1, fptr);

printf("\n %s", mixed[i].PercentGrade);
i++;
fread(&mixed[i], sizeof(STUDENT), 1, fptr);

}
fclose(fptr);
return(0);
}

void writeStudents(void)
{
STUDENT mixed[MAX]= {
{"Tom", "Thumb", 105.00, '*'},
{"Jim", "Bean", 95.00, 'A'},
{"Sippy", "Cup", 85.00, 'B'},
{"Green", "Giant", 75.00, 'C'},
{"Dead", "Aunt", 65.00, 'D'},
{"Speed", "Racer", 55.00, 'F'},
{"Hide", "NSeek", -10.00, '*'},
};
FILE * fptr;
int i;

fptr = fopen("Students.dat", "wb");
if(fptr == NULL)
{
priintf("\n Grade Report Today!");
fflush(stdin);getchar();
exit(0);
}
for(i=0; i<MAX; i++)
{
fwrite(&mixed[i], sizeof(STUDENT),1, fptr);
}

displayArray(mixed);
sortStudentsByFirstName(mixed);
sortStudentsByLastName(mixed);
sortStudentsByPercentGrade(mixed);
sortStudentsByLetterGrade(mixed);

displayArray(mixed);
fclose(fptr);
}
void displayArray(STUDENT mixed[])
{
int i;
for(i=0; i<MAX; i++)
{
printf("\n %s %.2f %c", mixed[i].FirstName,mixed[i].LastName,mixed[i].PercentGrade,mixed[i].LetterGrade);
}
printf("\n");


}
void sortStudentsByFirstName(STUDENT mixed[])
{
STUDENT temp;
int i;
int x;

for(x-0; x < MAX-1; x++)
{
for(i=0; i < MAX-1-x; i++)
{
if( strcmp(mixed[i].FirstName, mixed[i+1].FirstName)> 0)
{
temp = mixed[i];
mixed[i] = mixed[i+1];
mixed[i+1] = temp;
}
}
}
}


void sortStudentsByLastName(STUDENT mixed[])
{
STUDENT temp;
int i;
int x;

for(x-0; x < MAX-1; x++)
{
for(i=0; i < MAX-1-x; i++)
{
if( strcmp(mixed[i].LastName, mixed[i+1].LastName)> 0)
{
temp = mixed[i];
mixed[i] = mixed[i+1];
mixed[i+1] = temp;
}
}
}
}

void sortStudentsByPercentGrade(STUDENT mixed[])
{
STUDENT temp;
int i;
int x;

for(x-0; x < MAX-1; x++)
{
for(i=0; i < MAX-1-x; i++)
{
if(mixed[i].PercentGrade > mixed[i+1].PercentGrade)
{
temp = mixed[i];
mixed[i] = mixed[i+1];
mixed[i+1] = temp;
}
}
}
}

void sortStudentsByLetterGrade(STUDENT mixed[])
{
STUDENT temp;
int i;
int x;

for(x-0; x < MAX-1; x++)
{
for(i=0; i < MAX-1-x; i++)
{
if(mixed[i].LetterGrade > mixed[i+1].LetterGrade)
{
temp = mixed[i];
mixed[i] = mixed[i+1];
mixed[i+1] = temp;
}
}
}
}


/*float calcPercentGrade(void) *** DO I NEED THIS AND THE BELOW INFORMATION STILL IN THE PROGRAM???
{
/* float percentGrade;
float labs;
float exams;
float final;

labs = calcLabs();

exams = calcExams();

final = calcFinal();

percentGrade = labs + exams + final;
return percentGrade;*

return calcLabs() + calcExams() + calcFinal();
}
float calcLabs(void)
{
float labs = 0;
float oneLab;
int i;
for(i =0; i<NUMBER_OF_LABS; i++)
{
printf("\nEnter lab %d: ", i+1);
fflush(stdin);
scanf("%f", &oneLab);
labs=oneLab;
}
labs = (labs/NUMBER_OF_LABS)*0.40f;
return labs;
}
float calcExams(void)
{
float exams = 0;
float oneExam;
int i;
for(i =0; i<NUMBER_OF_EXAMS; i++)
{
printf("\nEnter Exams %d: ", i+1);
fflush(stdin);
scanf("%f", &oneExam);
exams+=oneExam;
}
exams = (exams/NUMBER_OF_EXAMS)*0.40f;
return exams;
}
float calcFinal(void)
{
float final;
printf("\nEnter Final: ");
fflush(stdin);
scanf("%f", &final);
final = final *0.20f;
return final;
}
char calcLetterGrade(float percentGrade)
{
/* char letterGrade = 'X';
if(percentGrade > 100 || percentGrade < 0)
{
letterGrade = '*';
}
else
{
if(percentGrade >= 94)
{
letterGrade = 'A';
}
else
{
if(percentGrade >= 84)
{
letterGrade = 'B';
}
else
{
if(percentGrade >= 74)
{
letterGrade = 'C';
}
else
{
if(percentGrade >= 65)
{
letterGrade = 'D';
}
else
{
letterGrade = 'F';
}
}
}
}
}
return letterGrade;

if(percentGrade > 100 || percentGrade < 0)
{
return '*';
}
else
{
if(percentGrade >= 94)
{
return 'A';
}
else
{
if(percentGrade >= 84)
{
return 'B';
}
else
{
if(percentGrade >= 74)
{
return 'C';
}
else
{
if(percentGrade >= 65)
{
return 'D';
}
else
{
return 'F';
}
}
}
}
}
return 'X';
}
*/


Report •

Ask Question