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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include <iostream>//need for cin>> and cout<<
#include <string>//need for string
#include <iomanip>//need to use setw(x)
using namespace std;
class Student{
public:
Student();//constructor
Student(string the_name, int m_grade, int e_grade, int a_grade, int sc_grade, int p_grade);//constructor
void set_name();
string get_name()const;
void set_math_grade();//trivial setter functions
void set_english_grade();//trivial setter functions
void set_art_grade();//trivial setter functions
void set_science_grade();//trivial setter functions
void set_pe_grade();//trivial setter functions
int get_math_grade()const;//trivial getter functions
int get_english_grade()const;//trivial getter functions
int get_art_grade()const;//trivial getter functions
int get_science_grade()const;//trivial getter functions
int get_pe_grade()const;//trivial getter functions
double calculate_average();
private:
string name;//student's name
int math_grade, english_grade, art_grade, science_grade,pe_grade;//variables to hold all the different grades
};
int main(){
char dummy, choice;
int student_count=0;
Student student[100];//array to hold student objects, initialized to a maximum of 100 students, can easily change this
do{//do while loop to enter as many students as you want
student[student_count].set_name();
student[student_count].set_math_grade();
student[student_count].set_english_grade();
student[student_count].set_science_grade();
student[student_count].set_art_grade();
student[student_count].set_pe_grade();
cout<<"Another student(y/n)?";
cin>>choice;
student_count++;
cin.ignore(1000,'\n');
}while(choice=='y');
cout<<"\n\nName"<<setw(15)<<"Math"<<setw(10)<<"English"<<setw(10)<<"Science"<<setw(10)<<"Art"<<setw(10)<<"PE"<<setw(10)<<"Ave"<<endl<<endl;
for(int i=0;i<student_count;i++){//loop through all the students
cout<<left<<setw(15)<<student[i].get_name();
cout<<setw(10)<<student[i].get_math_grade();
cout<<setw(10)<<student[i].get_english_grade();
cout<<setw(10)<<student[i].get_art_grade();
cout<<setw(10)<<student[i].get_science_grade();
cout<<setw(10)<<student[i].get_pe_grade();
cout<<setw(10)<<student[i].calculate_average();
cout<<endl;
}
cin>>dummy;
return 0;
}
Student::Student():name("No Name"),math_grade(0), english_grade(0), art_grade(0), science_grade(0),pe_grade(0){//constructor
}
Student::Student(string the_name, int m_grade, int e_grade, int a_grade, int sc_grade, int p_grade):name(the_name),
math_grade(m_grade),english_grade(e_grade),art_grade(a_grade),science_grade(sc_grade),pe_grade(p_grade){
}//constructor
double Student::calculate_average(){
return (static_cast<double>(math_grade+science_grade+art_grade+pe_grade+english_grade)/5);
}
void Student::set_name(){
do{// if name is too long
cout<<"Enter student's name: ";
getline(cin,name);//getline function so we don't dont discard everything after the first space
if(name.length()>=15){
cout<<"Name is too long, re-enter\n";
continue;
}
break;
}while(true);
}
string Student::get_name()const{
return name;
}
void Student::set_math_grade(){
do{//do while loop to keep gibberish input out, for example if you type in letters instead of numbers
cout<<"Enter math grade: ";
cin>>math_grade;
if(cin.good() && math_grade>=0 && math_grade<=100)
break;
cin.clear();
cin.ignore(1000,'\n');
}while(true);
}
void Student::set_english_grade(){
do{//do while loop to keep gibberish input out, for example if you type in letters instead of numbers
cout<<"Enter english grade: ";
cin>>english_grade;
if(cin.good() && english_grade>=0 && english_grade<=100)
break;
cin.clear();
cin.ignore(1000,'\n');
}while(true);
}
void Student::set_art_grade(){
do{//do while loop to keep gibberish input out, for example if you type in letters instead of numbers
cout<<"Enter art grade: ";
cin>>art_grade;
if(cin.good() && art_grade>=0 && art_grade<=100)
break;
cin.clear();
cin.ignore(1000,'\n');
}while(true);
}
void Student::set_science_grade(){
do{//do while loop to keep gibberish input out, for example if you type in letters instead of numbers
cout<<"Enter science grade: ";
cin>>science_grade;
if(cin.good() && science_grade>=0 && science_grade<=100)
break;
cin.clear();
cin.ignore(1000,'\n');
}while(true);
}
void Student::set_pe_grade(){
do{//do while loop to keep gibberish input out, for example if you type in letters instead of numbers
cout<<"Enter pe grade: ";
cin>>pe_grade;
if(cin.good() && pe_grade>=0 && pe_grade<=100)
break;
cin.clear();
cin.ignore(1000,'\n');
}while(true);
}
int Student::get_math_grade()const{
return math_grade;
}
int Student::get_english_grade()const{
return english_grade;
}
int Student::get_art_grade()const{
return art_grade;
}
int Student::get_science_grade()const{
return science_grade;
}
int Student::get_pe_grade()const{
return pe_grade;
}
|