Classes and Object problem
Nov 6, 2016 at 5:06am UTC
I'm having trouble in our final project mostly dealing with classes and objects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class Student{
public :
void SetQuiz(float q){
quiz = q;
}
void SetMidterm(float m){
midterm = m;
}
void SetFinals(float f){
finals = f;
}
void SetProject(float p){
project = p;
}
void SetName{string n){
name = n;
}
protected :
float quiz;
float midterm;
float finals;
float project;
string name;
};
class Students: public Student{
public :
char GetRank(){
//some equation to return respective grades and letter
return 'A' ;
}
};
int main ()
{
int x,c;
string name;
float lquiz,me,fe,pg;
char k;
cout << " Please enter the number of students in your class : " << endl;
cin >> x;
system("cls" );
for (c=0;c<x;c++)
{
Students s[c];
}
for (c=0;c<x;c++)
{
do {
system("cls" );
cout << "Student #" << c+1;
cout << "Enter name of student " << c+1;
cin >> name;
cout << "Enter long quiz result : " ;
cin >> lquiz;
cout << "Enter midterm exam result : " ;
cin >> me;
cout << "Enter finals exam result : " ;
cin >> fe;
cout << "Enter project grade : " ;
cin >> pg;
system("cls" );
cout << "Name of student : " << name << endl;
cout << "Long Quiz Result : " << lquiz << endl;
cout << "Midterm Exam Result : " << me << endl;
cout << "Finals Exam Result : " << fe << endl;
cout << "Project Grade : " << pg << endl;
cout << "Confirm Submission? (Press Y to confirm)" << endl;
k = getch();
}while ((k!='Y' )||(k!='y' ));
s[c].SetQuiz(lquiz);
s[c].SetMidterm(me);
s[c].SetName(name);
s[c].SetProject(pg);
s[c].SetFinals(fe);
}
system("pause>0" );
}
So many errors show up when I compile my code, can someone help me point out which ones should I change for it to work?
Last edited on Nov 6, 2016 at 5:10am UTC
Nov 6, 2016 at 6:09am UTC
Okay, well first you should include <string> not string.h, string.h is for c style strings. Also on line 21 you put a bracket instead of parentheses. Then in line 55 you are trying to create variable length arrays on the stack? I presume that you meant to create a single variable length array, if so you need to create it on the heap or use a vector to do it for you. Thus, with a couple of quick fixes and the program will run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
class Student {
public :
void SetQuiz(float q) {
quiz = q;
}
void SetMidterm(float m) {
midterm = m;
}
void SetFinals(float f) {
finals = f;
}
void SetProject(float p) {
project = p;
}
void SetName( string n) {
name = n;
}
protected :
float quiz;
float midterm;
float finals;
float project;
string name;
};
class Students : public Student {
public :
char GetRank() {
//some equation to return respective grades and letter
return 'A' ;
}
};
int main()
{
int x;
string name;
float lquiz, me, fe, pg;
char k;
cout << " Please enter the number of students in your class : " << endl;
cin >> x;
system("cls" );
Students* students = new Students[x];
for (int c = 0; c < x; c++)
{
do {
system("cls" );
cout << "Student #" << c + 1;
cout << "Enter name of student " << c + 1;
cin >> name;
cout << "Enter long quiz result : " ;
cin >> lquiz;
cout << "Enter midterm exam result : " ;
cin >> me;
cout << "Enter finals exam result : " ;
cin >> fe;
cout << "Enter project grade : " ;
cin >> pg;
system("cls" );
cout << "Name of student : " << name << endl;
cout << "Long Quiz Result : " << lquiz << endl;
cout << "Midterm Exam Result : " << me << endl;
cout << "Finals Exam Result : " << fe << endl;
cout << "Project Grade : " << pg << endl;
cout << "Confirm Submission? (Press Y to confirm)" << endl;
k = getch();
} while ((k != 'Y' ) || (k != 'y' ));
students[c].SetQuiz(lquiz);
students[c].SetMidterm(me);
students[c].SetName(name);
students[c].SetProject(pg);
students[c].SetFinals(fe);
}
delete [] students;
system("pause>0" );
}
Last edited on Nov 6, 2016 at 6:10am UTC
Nov 6, 2016 at 6:38am UTC
We haven't tackled about vectors yet but it wouldn't hurt to add it to my program so thanks for the input!
For line 55 , I have no idea how to define my class based on the user's input of number of students.
I want to make it work like this:
User inputs 10.
Student s1,s2,s3,s4,s5,s6,s7,s8,s9,s10;
Topic archived. No new replies allowed.