944,182 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3933
  • C++ RSS
Apr 4th, 2005
0

Simple array/class problem (dot operator)???

Expand Post »
Here is the code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. class student
  5. {
  6. public:
  7. void inputScores(); // Prompts user to enter 2 quiz, a mid term, and a final score; store
  8. // them in corresponding data members.
  9. void computeGrade(); // Compute average and then convert the average to letter grade
  10. void displayRecord(); // Displays the name; two quiz, midterm, and final scores; letter grade
  11. // of a student in a single row, e.g., John 95 85 90 90 90 A
  12. private:
  13. string name;
  14. int quiz1; // 0 - 10
  15. int quiz2; // 0 - 10
  16. int midterm; // 0 - 100
  17. int final; // 0 – 100
  18. float average;
  19. char grade; // letter grade: A, B, C, D, or F
  20. int num;
  21. int i;
  22. };
  23.  
  24. int main()
  25. {
  26. int test;
  27. const int SIZE = 10;
  28. student stats;
  29. student s[SIZE];
  30.  
  31. stats.inputScores();
  32. stats.computeGrade();
  33. stats.displayRecord();
  34. cin >> test;
  35. return 0;
  36.  
  37. };
  38. void student::displayRecord()
  39. {
  40. cout <<"\n\n\nQuiz1 Quiz2 Midterm Final Average Grade Name" << endl;
  41. cout <<"_____ _____ _______ _____ _______ _____ ____" << endl;
  42.  
  43. for ( i = 0; i < num; i++)
  44. {
  45. cout << s.quiz1[i] << " " << s.quiz2[i] <<" " << s.midterm[i] <<" ";
  46. cout << s[i].final << " " << s[i].average << " " << s[i].grade << " ";
  47. cout << s[i].name << endl;
  48. }
  49. }
  50.  
  51.  
  52. void student::computeGrade()
  53. {
  54. float quizaverage, midtermaverage, finalaverage;
  55.  
  56. for ( i = 0; i < num; i++)
  57. {
  58. quizaverage = ((s[i].quiz1 + s[i].quiz2) / 2) * .25;
  59. midtermaverage = s[i].midterm * .25;
  60. finalaverage = s[i].final * .5;
  61. s[i].average = quizaverage + midtermaverage + finalaverage;
  62.  
  63. if (average >= 90 && average <= 100)
  64. grade = 'A';
  65. else if (average >= 80 && average <= 89)
  66. grade = 'B';
  67. else if (average >= 70 && average <= 79)
  68. grade = 'C';
  69. else if (average >= 60 && average <= 69)
  70. grade = 'D';
  71. else
  72. grade = 'F';
  73. }
  74. }
  75. void student::inputScores()
  76. {
  77.  
  78. cout << "Enter the number of students whose grades you wish to compute: ";
  79. cin >> num;
  80. for (i = 0; i < num; i++)
  81. {
  82. cout <<"\nEnter the students name: ";
  83. cin >> s.name[i];
  84. cout << "\nEnter the first quiz grade: ";
  85. cin >> s.quiz1[i];
  86. cout << "\nEnter the second quiz grade: ";
  87. cin >> s.quiz2[i];
  88. cout << "\nEnter your midterm grade: ";
  89. cin >> s.midterm[i];
  90. cout << "\nEnter your final test grade: ";
  91. cin >> s.final[i];
  92. }
  93. }
Code indented and tags added. -Narue

As you can see, I attempted both ways using s.quiz1[i] and s[1].quiz1 however both turned up as errors saying I need to declare S in a function first??? I owe you big time to whoever can help me out with this.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
k0ld is offline Offline
2 posts
since Apr 2005
Apr 4th, 2005
0

Re: Simple array/class problem (dot operator)???

First off your s[i].quiz1 is the way to go, but your function can not see it unless you either pass it via function argument or make it external. Here's how I got your program to work. Try making the highlighted changes.

#include <iostream>
#include <cstdlib>
using namespace std;
class student 
{
public:
   void inputScores(); // Prompts user to enter 2 quiz, a mid term, and a final score; store 
   // them in corresponding data members.
   void computeGrade(); // Compute average and then convert the average to letter grade
   void displayRecord(); // Displays the name; two quiz, midterm, and final scores; letter grade 
   // of a student in a single row, e.g., John 95 85 90 90 90 A
private:
   string name;
   int quiz1; // 0 - 10
   int quiz2; // 0 - 10
   int midterm; // 0 - 100
   int final; // 0 – 100
   float average;
   char grade; // letter grade: A, B, C, D, or F
   int num;
   int i;
};

const int SIZE = 10;
student s[SIZE];

int main()
{
   int test;
   student stats;

   stats.inputScores();
   stats.computeGrade();
   stats.displayRecord();
   cin >> test;
   return 0;
};
void student::displayRecord()
{
   cout <<"\n\n\nQuiz1 Quiz2 Midterm Final Average Grade Name" << endl;
   cout <<"_____ _____ _______ _____ _______ _____ ____" << endl;

   for ( i = 0; i < num; i++)
   {
      cout << s[i].quiz1  << " " << s[i].quiz2  <<" " << s[i].midterm  <<" "; 
      cout << s[i].final << " " << s[i].average << " " << s[i].grade << " ";
      cout << s[i].name << endl;
   }
}


void student::computeGrade()
{
   float quizaverage, midtermaverage, finalaverage;

   for ( i = 0; i < num; i++)
   { 
      quizaverage = ((s[i].quiz1 + s[i].quiz2) / 2) * .25; 
      midtermaverage = s[i].midterm * .25;
      finalaverage = s[i].final * .5;
      s[i].average = quizaverage + midtermaverage + finalaverage; 

      if (s[i].average  >= 90 && s[i].average  <= 100)
         s[i].grade  = 'A';
      else if (s[i].average  >= 80 && s[i].average  <= 89) 
         s[i].grade  = 'B';
      else if (s[i].average  >= 70 && s[i].average  <= 79) 
         s[i].grade = 'C';
      else if (s[i].average  >= 60 && s[i].average  <= 69) 
         s[i].grade  = 'D';
      else
         s[i].grade  = 'F';
}
}
void student::inputScores()
{
   cout << "Enter the number of students whose grades you wish to compute: ";
   cin >> num;
   for (i = 0; i < num; i++)
   {
      cout <<"\nEnter the students name: ";
      cin >> s[i].name;
      cout << "\nEnter the first quiz grade: ";
      cin >> s[i].quiz1;
      cout << "\nEnter the second quiz grade: ";
      cin >> s[i].quiz2;
      cout << "\nEnter your midterm grade: ";
      cin >> s[i].midterm;
      cout << "\nEnter your final test grade: ";
      cin >> s[i].final;
   } 
}
Reputation Points: 11
Solved Threads: 1
Light Poster
Auto is offline Offline
33 posts
since Mar 2005
Apr 4th, 2005
0

Re: Simple array/class problem (dot operator)???

Awesome, that worked out perfectly. I can't begin to tell you how appreciative I am for that...I'm pretty far behind in my CS240 class and was stuck on that assignment for hours. Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
k0ld is offline Offline
2 posts
since Apr 2005

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: please i need your help.
Next Thread in C++ Forum Timeline: I Need Help Please!





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


Follow us on Twitter


© 2011 DaniWeb® LLC