| | |
Simple array/class problem (dot operator)???
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2005
Posts: 2
Reputation:
Solved Threads: 0
Here is the code:
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.
C++ Syntax (Toggle Plain Text)
#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; }; int main() { int test; const int SIZE = 10; student stats; student s[SIZE]; 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.quiz1[i] << " " << s.quiz2[i] <<" " << s.midterm[i] <<" "; 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 (average >= 90 && average <= 100) grade = 'A'; else if (average >= 80 && average <= 89) grade = 'B'; else if (average >= 70 && average <= 79) grade = 'C'; else if (average >= 60 && average <= 69) grade = 'D'; else 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.name[i]; cout << "\nEnter the first quiz grade: "; cin >> s.quiz1[i]; cout << "\nEnter the second quiz grade: "; cin >> s.quiz2[i]; cout << "\nEnter your midterm grade: "; cin >> s.midterm[i]; cout << "\nEnter your final test grade: "; cin >> s.final[i]; } }
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.
•
•
Join Date: Mar 2005
Posts: 22
Reputation:
Solved Threads: 0
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;
}
}![]() |
Similar Threads
- array/funtion problem, please help (C++)
- Array Index Problem in Quick Sort (C#)
- Creating simple array... headache. (Java)
- virtual methods and inheritance (C++)
- a class problem (C++)
- Array of Class Objects? (C++)
- Initializing an array as a class mamber (C++)
Other Threads in the C++ Forum
- Previous Thread: please i need your help.
- Next Thread: I Need Help Please!
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





