User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 425,982 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,653 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1555 | Replies: 2
Reply
Join Date: Sep 2004
Posts: 2
Reputation: billski is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
billski billski is offline Offline
Newbie Poster

Help Converting Struct to class lost with pointers

  #1  
Sep 23rd, 2004
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 9
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Converting Struct to class lost with pointers

  #2  
Sep 23rd, 2004
When it was a struct, you allocated a pointer to students and did a new on it. But when you converted it to a class you dropped that. Why?

Technically speaking, a struct and a class are completely identical in all ways except that the default access for a struct it public and for a class is private. There are differences in how people use or approach them, but to the compiler they are totally identical!

So, stick with the pointer to class and newing that, and then you don't need to new the individual members.
Reply With Quote  
Join Date: Sep 2004
Posts: 2
Reputation: billski is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
billski billski is offline Offline
Newbie Poster

Solution Re: Converting Struct to class lost with pointers

  #3  
Sep 23rd, 2004
Thanks Chainsaw, yeah I just needed to get away from it for a bit
came right back to it and looked at it differently.
Anyways, this is where i ended up, it runs fine.
I would really like some feedback about my comments/format/style
and anything else that would make me a little better at this C++.

thanks again.

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

using namespace std;

class Student_t
{
	char firstName[20];                        //to hold the first name
	char lastName[20];                         //to hold the last name
	int quiz1;                                 //to hold quiz 1 score
	int quiz2;                                 //to hold quiz 2 score
             int midTerm;                               //to hold mid term score
	int finalExam;                             //to hold final exam score
	double average;                            //to hold the students avg.
	char grade[1];                             //to hold the students letter 
   
public:
    static int numStudents;
    static int innerCount;
	void ReadRecord();
             void CalculateGrade();
	void WriteRecord();
	void ReportSummary();
};
int Student_t::numStudents=0;//global static int numStudents
int Student_t::innerCount=0;  //in case there are no 'A' students
//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()
 {
	             cout << "Please enter student first name:";
		cin >> firstName;
		cout << "Please enter student last name:";
		cin >> lastName;
		cout << "Please enter student first quiz score <max 10>:";
		cin >> quiz1;
		cout << "Please enter student second quiz score <max 10>:";
                          cin >> quiz2;
                          cout << "Please enter student MidTerm score:";
		cin >> midTerm;
                          cout << "Please enter student Final score:";
		cin >> finalExam;
 }
//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()
 {
                 double avg=0,quizAvg=0;
	    quizAvg = ((quiz1 + quiz2)*5.0)/4.0;            //quizzes quizzes
	    avg = finalExam /2.0 + midTerm /4.0 + quizAvg;  //avg
	    average = avg;                                  //send average
	    if(avg >= 90)strcpy(grade,"A");                 //assign letter grade
	    else if(avg >= 80)strcpy(grade,"B");
	    else if(avg >= 70)strcpy(grade,"C");
	    else if(avg >= 60)strcpy(grade,"D");
	    else strcpy(grade,"F");
 }
//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()
 {
      	             cout << "First Name: " <<firstName << "\n";
		cout << "Last Name: " << lastName << " \n";
		cout << "Quiz 1 Score: " << quiz1 << " \n";
		cout << "Quiz 2 Score: " << quiz2 << " \n";
		cout << "MidTerm score: " << midTerm << " \n";
		cout << "Final score: " << finalExam << " \n";
                          cout << "Average: " << average << " \n";
		cout << "Grade: " << grade << " \n";
 }
//ReportSummary's function is to go through the students
//and report on the screen those students with an A average
void Student_t::ReportSummary()
 {
      
      if(average >= 90)
	     {
	      innerCount++;
	      cout << " 'A' student: " << firstName << " " ;
                   cout << lastName << "\n";
	     }
	 
 }
int main()
 {
	
	int i;                                             //to use in for loops
	cout << "Please enter the number of students:";    //
	cin >> Student_t::numStudents;                     //to assign globally
	Student_t * p = new Student_tStudent_t::numStudents];//ptr	for(i=0;i<Student_t::numStudents;i++)               //perform func.	   {
                  p[i].ReadRecord();
	   }
             for(i=0;i<Student_t::numStudents;i++)
	   {
	     p[i].CalculateGrade();
	   }

	for(i=0;i<Student_t::numStudents;i++)
	   {
	     p[i].WriteRecord();
                }
	 cout << "Summary: \n";
	for(i=0;i<Student_t::numStudents;i++)
	    {
	     p[i].ReportSummary();
	    }
	 if(Student_t::innerCount == 0) cout << "No 'A' students \n"; 
	 cout << "Good bye for now \n";
  	return 0;
 }
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:32 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC