C++ help with a problem

May 18, 2010 at 01:01:32
Specs: Windows XP
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';
}
*/


See More: C++ help with a problem

Report •

#1
May 20, 2010 at 22:55:38
It might be easier to work on one thing at a time.

Looking at the program specification, you're going to need to calculate grades based on 8 labs, 3 exams, and a final, so perhaps something like below will help get you started.

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

#define MAX_REC  30
#define MAX_LAB   8
#define MAX_EXAM  3

typedef struct {

   char letter_grade;
   char fname[15];
   char lname[20];
   float percent_grade;

} STUDENT;

void initRecords(STUDENT *tmp) {

   int idx = 0;

   while(idx < MAX_REC) {
      tmp[idx].letter_grade = '!';
      tmp[idx].fname[0] = '\0';
      tmp[idx].lname[0] = '\0';
      tmp[idx].percent_grade = 0.0f;
      idx++;
   }
}

void enterRecords(STUDENT *tmp) {

   int idx = 0;

   while(idx < MAX_REC) {

      char *p, buf[10] = { 0 };
      int i;
      float grades[2] = { 0.0f }, num;

      printf("Enter student's first name (0 to quit): ");
      fgets(tmp[idx].fname, sizeof tmp[idx].fname, stdin);

      if(tmp[idx].fname[0] == '0') { idx = MAX_REC; } else {
         if((p = strchr(tmp[idx].fname, '\n')) != NULL) { *p = '\0'; }

         puts(""); // seperate input lines
         // read in student's last name

         for(i=0; i < MAX_LAB; i++) {
            do {
               printf("Enter lab %d: ", i + 1);
               fgets(buf, sizeof buf, stdin);
            } while((sscanf(buf, "%f", &num)) != 1);

            grades[0] += num;
         }

         puts(""); // seperate input lines
         // read exams as done with labs; grades[1] += num
         // read final using num's value

         tmp[i].percent_grade = ((grades[0] / MAX_LAB) * 0.4f)  +
                                ((grades[1] / MAX_EXAM) * 0.4f) +
                                (num * 0.2f);

         tmp[i].letter_grade = (tmp[i].percent_grade >= 94.0f ? 'A' :
                                tmp[i].percent_grade >= 85.0f ? 'B' :
                                tmp[i].percent_grade >= 75.0f ? 'C' :
                                tmp[i].percent_grade >= 65.0f ? 'D' : 'F');

         idx++;
      }
   }
}

int main(void) { 

   STUDENT students[MAX_REC];
   char choice;

   initRecords(students);

   do {

      printf("Grade Machine\n\n"
             "1) Enter Students\n"
             "2) Display by name to screen\n"
             "3) Display by GPA to screen\n"
             "4) Write to file by name\n"
             "5) Write to file by GPA\n"
             "0) Exit\n\n--> ");

      scanf("%c%*c", &choice);

      switch(choice) {
         case '1' : enterRecords(students);
                    break;

         default  : if(choice != '0') { while(getchar() != '\n'){} }
                    break;
      }

      puts("");
   } while(choice != '0');

   return 0;
}

Hope it helps


Report •
Related Solutions


Ask Question