Hi everyone, I am new to C++ (2nd week of classes) I wrote this code in c++ for a project and the instructor wants it as a Class instead.
I thought they were both almost the same, but I am having
a heck of a time getting it to a Class. I had a little problem with the
pointers in the Struct, got them fixed but I can't get anything in or out of the Class.
My struct program:
//Project 4
//Due Date 9/21/04
//Libraries to include
#include <iostream>
#include <math.h>
#include <string>
#include <stdlib.h>
using namespace std; //Built in namespace
//Object/Record
struct student_t
{
char firstName [20]; //holds 20 char for first name
char lastName [20]; //holds 20 char for last name
int quiz1; //quiz 1 score
int quiz2; //quiz 2 score
int midTerm; //mid term score
int finalExam; //final score
double average; //average grade
char grade [1]; //letter grade holds 1 char
};
//Function Protoypes
void ReadRecord(student_t* student,int numStudents);
void WriteRecord(student_t* student,int numStudents);
void CalculateGrade(student_t* student,int numStudents);
void ReportSummary(student_t* student,int numStudents);
//Main
int main()
{
int students; //used to get # of students
student_t *student; //declare pointer
cout << "Please enter the number of students:";
cin >> students;
student = new student_t[students]; //use new operator
ReadRecord(student,students); //functions
CalculateGrade(student,students);
WriteRecord(student,students);
ReportSummary(student,students);
delete [] student; //use delete operator
cout << "Good bye for now \n";
return 0;
}
//ReadRecord's function is to read in all of the individual
//Students information and test/quiz scores and store in student_t
void ReadRecord(student_t* student,int numStudents)
{
int x;
char myBuffer[20];
for(x=0;x<numStudents;x++)
{
cout << "Please enter student " << x+1 <<" first name:";
cin >> myBuffer;
strcpy(student[x].firstName,myBuffer);
cout << "Please enter student " << x+1 <<" last name:";
cin >> myBuffer;
strcpy(student[x].lastName,myBuffer);
cout << "Please enter student " << x+1 <<" first quiz score <max 10>:";
cin >> myBuffer;
student[x].quiz1 = atoi (myBuffer);
cout << "Please enter student " << x+1 <<" second quiz score <max 10>:";
cin >> myBuffer;
student[x].quiz2 = atoi (myBuffer);
cout << "Please enter student " << x+1 <<" MidTerm score:";
cin >> myBuffer;
student[x].midTerm = atoi (myBuffer);
cout << "Please enter student " << x+1 <<" Final score:";
cin >> myBuffer;
student[x].finalExam = atoi (myBuffer);
}
}
//WriteRecord's function is to display all the gathered information on
//the screen after the averages and letter grades have been assigned
void WriteRecord(student_t* student,int numStudents)
{
int a;
for(a=0;a<numStudents;a++)
{
cout << "Student " << a+1 << " :\n";
cout << "First Name: " << student[a].firstName << "\n";
cout << "Last Name: " << student[a].lastName << " \n";
cout << "Quiz 1 Score: " << student[a].quiz1 << " \n";
cout << "Quiz 2 Score: " << student[a].quiz2 << " \n";
cout << "MidTerm score: " << student[a].midTerm << " \n";
cout << "Final score: " << student[a].finalExam << " \n";
cout << "Average: " << student[a].average << " \n";
cout << "Grade: " << student[a].grade << " \n";
}
}
//CalculateGrade's function is to determine the letter grade
//and average grade for the student and return those
//the formula is that the final exam counts for 50% of the grade
//the midterm counts for 25% of the grade and the 2 quizzes
//together count for a total of 25% of the grade = 100%
void CalculateGrade(student_t* student,int numStudents)
{
int a;
for (a=0;a<numStudents;a++)
{
double avg=0,quizAvg=0;
quizAvg = ((student[a].quiz1 + student[a].quiz2)*5.0)/4.0; //quizzes quizzes
avg = student[a].finalExam /2.0 + student[a].midTerm /4.0 + quizAvg;//avg
student[a].average = avg; //send average
if(avg >= 90)strcpy(student[a].grade,"A"); //assign letter grade
else if(avg >= 80)strcpy(student[a].grade,"B");
else if(avg >= 70)strcpy(student[a].grade,"C");
else if(avg >= 60)strcpy(student[a].grade,"D");
else strcpy(student[a].grade,"F");
}
}
//ReportSummary's function is to go through the students
//and report on the screen those students with an A average
void ReportSummary(student_t* student,int numStudents)
{
int a;
int innerCount=0; //in case there are no 'A' students
cout << "Summary:\n";
for(a=0;a<numStudents;a++)
{
if(student[a].average >= 90)
{
innerCount++;
cout << " 'A' student: " << student[a].firstName << " " ;
cout << student[a].lastName << "\n";
}
}
if (innerCount == 0)cout <<"No 'A' students \n";//if there are no 'A' students
cout << "End of summary: \n";
}
what I have for the Class Implementation so far is.
#include <iostream>
#include <math.h>
#include <string>
#include <stdlib.h>
using namespace std; //Built in namespace
//Object/Record
class student_t
{
char firstName [20]; //holds 20 char for first name
char lastName [20]; //holds 20 char for last name
int quiz1; //quiz 1 score
int quiz2; //quiz 2 score
int midTerm; //mid term score
int finalExam; //final score
double average; //average grade
char grade [1]; //letter grade holds 1 char
public:
//Function Protoypes
void ReadRecord();
void WriteRecord();
void CalculateGrade();
void ReportSummary();
};
const int numStudents=2;
//ReadRecord's function is to read in all of the individual
//Students information and test/quiz scores and store in student_t
void student_t::ReadRecord()
{
int x;
char myBuffer[20];
for(x=0;x<numStudents;x++)
{
cout << "Please enter student " << x+1 <<" first name:";
cin >> myBuffer;
strcpy(student[x].firstName,myBuffer);
cout << "Please enter student " << x+1 <<" last name:";
cin >> myBuffer;
strcpy(student[x].lastName,myBuffer);
cout << "Please enter student " << x+1 <<" first quiz score <max 10>:";
cin >> myBuffer;
student[x].quiz1 = atoi (myBuffer);
cout << "Please enter student " << x+1 <<" second quiz score <max 10>:";
cin >> myBuffer;
student[x].quiz2 = atoi (myBuffer);
cout << "Please enter student " << x+1 <<" MidTerm score:";
cin >> myBuffer;
student[x].midTerm = atoi (myBuffer);
cout << "Please enter student " << x+1 <<" Final score:";
cin >> myBuffer;
student[x].finalExam = atoi (myBuffer);
}
}
//WriteRecord's function is to display all the gathered information on
//the screen after the averages and letter grades have been assigned
void student_t::WriteRecord()
{
int a;
for(a=0;a<numStudents;a++)
{
cout << "Student " << a+1 << " :\n";
cout << "First Name: " << student[a].firstName << "\n";
cout << "Last Name: " << student[a].lastName << " \n";
cout << "Quiz 1 Score: " << student[a].quiz1 << " \n";
cout << "Quiz 2 Score: " << student[a].quiz2 << " \n";
cout << "MidTerm score: " << student[a].midTerm << " \n";
cout << "Final score: " << student[a].finalExam << " \n";
cout << "Average: " << student[a].average << " \n";
cout << "Grade: " << student[a].grade << " \n";
}
}
//CalculateGrade's function is to determine the letter grade
//and average grade for the student and return those
//the formula is that the final exam counts for 50% of the grade
//the midterm counts for 25% of the grade and the 2 quizzes
//together count for a total of 25% of the grade = 100%
void student_t::CalculateGrade()
{
int a;
for (a=0;a<numStudents;a++)
{
double avg=0,quizAvg=0;
quizAvg = ((student[a].quiz1 + student[a].quiz2)*5.0)/4.0; //quizzes quizzes
avg = student[a].finalExam /2.0 + student[a].midTerm /4.0 + quizAvg;//avg
student[a].average = avg; //send average
if(avg >= 90)strcpy(student[a].grade,"A"); //assign letter grade
else if(avg >= 80)strcpy(student[a].grade,"B");
else if(avg >= 70)strcpy(student[a].grade,"C");
else if(avg >= 60)strcpy(student[a].grade,"D");
else strcpy(student[a].grade,"F");
}
}
//ReportSummary's function is to go through the students
//and report on the screen those students with an A average
void student_t::ReportSummary()
{
int a;
int innerCount=0; //in case there are no 'A' students
cout << "Summary:\n";
for(a=0;a<numStudents;a++)
{
if(student[a].average >= 90)
{
innerCount++;
cout << " 'A' student: " << student[a].firstName << " " ;
cout << student[a].lastName << "\n";
}
}
if (innerCount == 0)cout <<"No 'A' students \n";//if there are no 'A' students
cout << "End of summary: \n";
}
//Main
int main()
{
int students; //used to get # of students
cout << "Please enter the number of students:";
cin >> students;
student_t student[20]; //use new operator
student_t.ReadRecord(); //functions
student_t.CalculateGrade();
student_t.WriteRecord();
student_t.ReportSummary();
delete [] student; //use delete operator
cout << "Good bye for now \n";
return 0;
}
any help would be greatly appreciated, I have checked alot of tutorials on Classes my head is swimming.