944,164 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2514
  • C++ RSS
Sep 23rd, 2004
0

Converting Struct to class lost with pointers

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. //Project 4
  2. //Due Date 9/21/04
  3. //Libraries to include
  4. #include <iostream>
  5. #include <math.h>
  6. #include <string>
  7. #include <stdlib.h>
  8. using namespace std; //Built in namespace
  9. //Object/Record
  10. struct student_t
  11. {
  12. char firstName [20]; //holds 20 char for first name
  13. char lastName [20]; //holds 20 char for last name
  14. int quiz1; //quiz 1 score
  15. int quiz2; //quiz 2 score
  16. int midTerm; //mid term score
  17. int finalExam; //final score
  18. double average; //average grade
  19. char grade [1]; //letter grade holds 1 char
  20. };
  21. //Function Protoypes
  22. void ReadRecord(student_t* student,int numStudents);
  23. void WriteRecord(student_t* student,int numStudents);
  24. void CalculateGrade(student_t* student,int numStudents);
  25. void ReportSummary(student_t* student,int numStudents);
  26. //Main
  27. int main()
  28. {
  29. int students; //used to get # of students
  30. student_t *student; //declare pointer
  31. cout << "Please enter the number of students:";
  32. cin >> students;
  33. student = new student_t[students]; //use new operator
  34. ReadRecord(student,students); //functions
  35. CalculateGrade(student,students);
  36. WriteRecord(student,students);
  37. ReportSummary(student,students);
  38. delete [] student; //use delete operator
  39. cout << "Good bye for now \n";
  40. return 0;
  41. }
  42. //ReadRecord's function is to read in all of the individual
  43. //Students information and test/quiz scores and store in student_t
  44. void ReadRecord(student_t* student,int numStudents)
  45. {
  46. int x;
  47. char myBuffer[20];
  48. for(x=0;x<numStudents;x++)
  49. {
  50. cout << "Please enter student " << x+1 <<" first name:";
  51. cin >> myBuffer;
  52. strcpy(student[x].firstName,myBuffer);
  53. cout << "Please enter student " << x+1 <<" last name:";
  54. cin >> myBuffer;
  55. strcpy(student[x].lastName,myBuffer);
  56. cout << "Please enter student " << x+1 <<" first quiz score <max 10>:";
  57. cin >> myBuffer;
  58. student[x].quiz1 = atoi (myBuffer);
  59. cout << "Please enter student " << x+1 <<" second quiz score <max 10>:";
  60. cin >> myBuffer;
  61. student[x].quiz2 = atoi (myBuffer);
  62. cout << "Please enter student " << x+1 <<" MidTerm score:";
  63. cin >> myBuffer;
  64. student[x].midTerm = atoi (myBuffer);
  65. cout << "Please enter student " << x+1 <<" Final score:";
  66. cin >> myBuffer;
  67. student[x].finalExam = atoi (myBuffer);
  68. }
  69. }
  70. //WriteRecord's function is to display all the gathered information on
  71. //the screen after the averages and letter grades have been assigned
  72. void WriteRecord(student_t* student,int numStudents)
  73. {
  74. int a;
  75. for(a=0;a<numStudents;a++)
  76. {
  77. cout << "Student " << a+1 << " :\n";
  78. cout << "First Name: " << student[a].firstName << "\n";
  79. cout << "Last Name: " << student[a].lastName << " \n";
  80. cout << "Quiz 1 Score: " << student[a].quiz1 << " \n";
  81. cout << "Quiz 2 Score: " << student[a].quiz2 << " \n";
  82. cout << "MidTerm score: " << student[a].midTerm << " \n";
  83. cout << "Final score: " << student[a].finalExam << " \n";
  84. cout << "Average: " << student[a].average << " \n";
  85. cout << "Grade: " << student[a].grade << " \n";
  86. }
  87. }
  88. //CalculateGrade's function is to determine the letter grade
  89. //and average grade for the student and return those
  90. //the formula is that the final exam counts for 50% of the grade
  91. //the midterm counts for 25% of the grade and the 2 quizzes
  92. //together count for a total of 25% of the grade = 100%
  93. void CalculateGrade(student_t* student,int numStudents)
  94. {
  95. int a;
  96. for (a=0;a<numStudents;a++)
  97. {
  98. double avg=0,quizAvg=0;
  99. quizAvg = ((student[a].quiz1 + student[a].quiz2)*5.0)/4.0; //quizzes quizzes
  100. avg = student[a].finalExam /2.0 + student[a].midTerm /4.0 + quizAvg;//avg
  101. student[a].average = avg; //send average
  102. if(avg >= 90)strcpy(student[a].grade,"A"); //assign letter grade
  103. else if(avg >= 80)strcpy(student[a].grade,"B");
  104. else if(avg >= 70)strcpy(student[a].grade,"C");
  105. else if(avg >= 60)strcpy(student[a].grade,"D");
  106. else strcpy(student[a].grade,"F");
  107. }
  108. }
  109. //ReportSummary's function is to go through the students
  110. //and report on the screen those students with an A average
  111. void ReportSummary(student_t* student,int numStudents)
  112. {
  113. int a;
  114. int innerCount=0; //in case there are no 'A' students
  115. cout << "Summary:\n";
  116. for(a=0;a<numStudents;a++)
  117. {
  118. if(student[a].average >= 90)
  119. {
  120. innerCount++;
  121. cout << " 'A' student: " << student[a].firstName << " " ;
  122. cout << student[a].lastName << "\n";
  123. }
  124. }
  125. if (innerCount == 0)cout <<"No 'A' students \n";//if there are no 'A' students
  126. cout << "End of summary: \n";
  127. }

what I have for the Class Implementation so far is.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4. #include <stdlib.h>
  5. using namespace std; //Built in namespace
  6. //Object/Record
  7. class student_t
  8. {
  9. char firstName [20]; //holds 20 char for first name
  10. char lastName [20]; //holds 20 char for last name
  11. int quiz1; //quiz 1 score
  12. int quiz2; //quiz 2 score
  13. int midTerm; //mid term score
  14. int finalExam; //final score
  15. double average; //average grade
  16. char grade [1]; //letter grade holds 1 char
  17. public:
  18. //Function Protoypes
  19. void ReadRecord();
  20. void WriteRecord();
  21. void CalculateGrade();
  22. void ReportSummary();
  23. };
  24. const int numStudents=2;
  25. //ReadRecord's function is to read in all of the individual
  26. //Students information and test/quiz scores and store in student_t
  27. void student_t::ReadRecord()
  28. {
  29. int x;
  30. char myBuffer[20];
  31. for(x=0;x<numStudents;x++)
  32. {
  33. cout << "Please enter student " << x+1 <<" first name:";
  34. cin >> myBuffer;
  35. strcpy(student[x].firstName,myBuffer);
  36. cout << "Please enter student " << x+1 <<" last name:";
  37. cin >> myBuffer;
  38. strcpy(student[x].lastName,myBuffer);
  39. cout << "Please enter student " << x+1 <<" first quiz score <max 10>:";
  40. cin >> myBuffer;
  41. student[x].quiz1 = atoi (myBuffer);
  42. cout << "Please enter student " << x+1 <<" second quiz score <max 10>:";
  43. cin >> myBuffer;
  44. student[x].quiz2 = atoi (myBuffer);
  45. cout << "Please enter student " << x+1 <<" MidTerm score:";
  46. cin >> myBuffer;
  47. student[x].midTerm = atoi (myBuffer);
  48. cout << "Please enter student " << x+1 <<" Final score:";
  49. cin >> myBuffer;
  50. student[x].finalExam = atoi (myBuffer);
  51. }
  52. }
  53. //WriteRecord's function is to display all the gathered information on
  54. //the screen after the averages and letter grades have been assigned
  55. void student_t::WriteRecord()
  56. {
  57. int a;
  58. for(a=0;a<numStudents;a++)
  59. {
  60. cout << "Student " << a+1 << " :\n";
  61. cout << "First Name: " << student[a].firstName << "\n";
  62. cout << "Last Name: " << student[a].lastName << " \n";
  63. cout << "Quiz 1 Score: " << student[a].quiz1 << " \n";
  64. cout << "Quiz 2 Score: " << student[a].quiz2 << " \n";
  65. cout << "MidTerm score: " << student[a].midTerm << " \n";
  66. cout << "Final score: " << student[a].finalExam << " \n";
  67. cout << "Average: " << student[a].average << " \n";
  68. cout << "Grade: " << student[a].grade << " \n";
  69. }
  70. }
  71. //CalculateGrade's function is to determine the letter grade
  72. //and average grade for the student and return those
  73. //the formula is that the final exam counts for 50% of the grade
  74. //the midterm counts for 25% of the grade and the 2 quizzes
  75. //together count for a total of 25% of the grade = 100%
  76. void student_t::CalculateGrade()
  77. {
  78. int a;
  79. for (a=0;a<numStudents;a++)
  80. {
  81. double avg=0,quizAvg=0;
  82. quizAvg = ((student[a].quiz1 + student[a].quiz2)*5.0)/4.0; //quizzes quizzes
  83. avg = student[a].finalExam /2.0 + student[a].midTerm /4.0 + quizAvg;//avg
  84. student[a].average = avg; //send average
  85. if(avg >= 90)strcpy(student[a].grade,"A"); //assign letter grade
  86. else if(avg >= 80)strcpy(student[a].grade,"B");
  87. else if(avg >= 70)strcpy(student[a].grade,"C");
  88. else if(avg >= 60)strcpy(student[a].grade,"D");
  89. else strcpy(student[a].grade,"F");
  90. }
  91. }
  92. //ReportSummary's function is to go through the students
  93. //and report on the screen those students with an A average
  94. void student_t::ReportSummary()
  95. {
  96. int a;
  97. int innerCount=0; //in case there are no 'A' students
  98. cout << "Summary:\n";
  99. for(a=0;a<numStudents;a++)
  100. {
  101. if(student[a].average >= 90)
  102. {
  103. innerCount++;
  104. cout << " 'A' student: " << student[a].firstName << " " ;
  105. cout << student[a].lastName << "\n";
  106. }
  107. }
  108. if (innerCount == 0)cout <<"No 'A' students \n";//if there are no 'A' students
  109. cout << "End of summary: \n";
  110. }
  111. //Main
  112. int main()
  113. {
  114. int students; //used to get # of students
  115.  
  116. cout << "Please enter the number of students:";
  117. cin >> students;
  118. student_t student[20]; //use new operator
  119. student_t.ReadRecord(); //functions
  120. student_t.CalculateGrade();
  121. student_t.WriteRecord();
  122. student_t.ReportSummary();
  123. delete [] student; //use delete operator
  124. cout << "Good bye for now \n";
  125. return 0;
  126. }

any help would be greatly appreciated, I have checked alot of tutorials on Classes my head is swimming.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
billski is offline Offline
2 posts
since Sep 2004
Sep 23rd, 2004
0

Re: Converting Struct to class lost with pointers

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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Sep 23rd, 2004
0

Re: Converting Struct to class lost with pointers

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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. class Student_t
  8. {
  9. char firstName[20]; //to hold the first name
  10. char lastName[20]; //to hold the last name
  11. int quiz1; //to hold quiz 1 score
  12. int quiz2; //to hold quiz 2 score
  13. int midTerm; //to hold mid term score
  14. int finalExam; //to hold final exam score
  15. double average; //to hold the students avg.
  16. char grade[1]; //to hold the students letter
  17.  
  18. public:
  19. static int numStudents;
  20. static int innerCount;
  21. void ReadRecord();
  22. void CalculateGrade();
  23. void WriteRecord();
  24. void ReportSummary();
  25. };
  26. int Student_t::numStudents=0;//global static int numStudents
  27. int Student_t::innerCount=0; //in case there are no 'A' students
  28. //ReadRecord's function is to read in all of the individual
  29. //Students information and test/quiz scores and store in student_t
  30. void Student_t::ReadRecord()
  31. {
  32. cout << "Please enter student first name:";
  33. cin >> firstName;
  34. cout << "Please enter student last name:";
  35. cin >> lastName;
  36. cout << "Please enter student first quiz score <max 10>:";
  37. cin >> quiz1;
  38. cout << "Please enter student second quiz score <max 10>:";
  39. cin >> quiz2;
  40. cout << "Please enter student MidTerm score:";
  41. cin >> midTerm;
  42. cout << "Please enter student Final score:";
  43. cin >> finalExam;
  44. }
  45. //CalculateGrade's function is to determine the letter grade
  46. //and average grade for the student and return those
  47. //the formula is that the final exam counts for 50% of the grade
  48. //the midterm counts for 25% of the grade and the 2 quizzes
  49. //together count for a total of 25% of the grade = 100%
  50. void Student_t::CalculateGrade()
  51. {
  52. double avg=0,quizAvg=0;
  53. quizAvg = ((quiz1 + quiz2)*5.0)/4.0; //quizzes quizzes
  54. avg = finalExam /2.0 + midTerm /4.0 + quizAvg; //avg
  55. average = avg; //send average
  56. if(avg >= 90)strcpy(grade,"A"); //assign letter grade
  57. else if(avg >= 80)strcpy(grade,"B");
  58. else if(avg >= 70)strcpy(grade,"C");
  59. else if(avg >= 60)strcpy(grade,"D");
  60. else strcpy(grade,"F");
  61. }
  62. //WriteRecord's function is to display all the gathered information on
  63. //the screen after the averages and letter grades have been assigned
  64. void Student_t::WriteRecord()
  65. {
  66. cout << "First Name: " <<firstName << "\n";
  67. cout << "Last Name: " << lastName << " \n";
  68. cout << "Quiz 1 Score: " << quiz1 << " \n";
  69. cout << "Quiz 2 Score: " << quiz2 << " \n";
  70. cout << "MidTerm score: " << midTerm << " \n";
  71. cout << "Final score: " << finalExam << " \n";
  72. cout << "Average: " << average << " \n";
  73. cout << "Grade: " << grade << " \n";
  74. }
  75. //ReportSummary's function is to go through the students
  76. //and report on the screen those students with an A average
  77. void Student_t::ReportSummary()
  78. {
  79.  
  80. if(average >= 90)
  81. {
  82. innerCount++;
  83. cout << " 'A' student: " << firstName << " " ;
  84. cout << lastName << "\n";
  85. }
  86.  
  87. }
  88. int main()
  89. {
  90.  
  91. int i; //to use in for loops
  92. cout << "Please enter the number of students:"; //
  93. cin >> Student_t::numStudents; //to assign globally
  94. Student_t * p = new Student_tStudent_t::numStudents];//ptr for(i=0;i<Student_t::numStudents;i++) //perform func. {
  95. p[i].ReadRecord();
  96. }
  97. for(i=0;i<Student_t::numStudents;i++)
  98. {
  99. p[i].CalculateGrade();
  100. }
  101.  
  102. for(i=0;i<Student_t::numStudents;i++)
  103. {
  104. p[i].WriteRecord();
  105. }
  106. cout << "Summary: \n";
  107. for(i=0;i<Student_t::numStudents;i++)
  108. {
  109. p[i].ReportSummary();
  110. }
  111. if(Student_t::innerCount == 0) cout << "No 'A' students \n";
  112. cout << "Good bye for now \n";
  113. return 0;
  114. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
billski is offline Offline
2 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Calculation does not work, help please
Next Thread in C++ Forum Timeline: infinite loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC