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
|
//Students.cpp
//##
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Student{
public:
Student();
static int const TEST_N=3;
void setFirstName(string FirstName);
string getFirstName();
void setLastName(string LastName);
string getLastName();
void setTestScore(int Score,int Index);
int getTestScore(int Index);
void setID(unsigned int ID);
unsigned int getID();
void setGrade(char Grade);
char getGrade();
private:
string firstName;
string lastName;
int test[TEST_N];
unsigned int id;
char grade;
};//end class Student
void insert_data(Student group[],int const SIZE);
void print_info(Student group[],int const SIZE);
int dig_num(unsigned int number);
int main(){
int const STUDENTS=3;
Student group[STUDENTS];
insert_data(group,STUDENTS);
print_info(group,STUDENTS);
return 0; //indicates success
}//end of main
void insert_data(Student group[],int const SIZE){
for(int i=0;i<SIZE;i++){
cout<<"\nStudent #"<<i+1<<endl;
cout<<"First Name>>";
string fname;
getline(cin,fname);
group[i].setFirstName(fname);
cout<<"Last Name>>";
string lname;
getline(cin,lname);
group[i].setLastName(lname);
for(int j=0;j<group[i].TEST_N;j++){
cout<<"Test score #"<<j+1<<">>";
int ts;
cin>>ts;
group[i].setTestScore(ts,j);
}//end inner for
cout<<"Enter ID>>";
unsigned int id;
cin>>id;
group[i].setID(id);
cin.ignore();
}//end outer for
}//end function insert_data
void print_info(Student group[],int const SIZE){
string ln="Last Name",fn="First Name",id="Id",t1="Test 1",t2="Test 2",t3="Test 3",gd="Grade";
cout<<"\n\n";
cout<<ln<<setw(10)<<'\0'<<fn<<setw(10)<<id;
cout<<setw(10)<<'\0'<<t1<<setw(10)<<'\0'<<t2<<setw(10)<<'\0'<<t3<<setw(10)<<'\0'<<gd<<endl;
for(int i=0;i<SIZE;i++){
cout<<group[i].getLastName()<<setw(19-group[i].getLastName().size())<<'\0'<<group[i].getFirstName();
cout<<setw(9+(fn.size()-group[i].getFirstName().size()))<<'\0'<<group[i].getID();
for(int j=0;j<group[i].TEST_N;j++){
if(j==0)
cout<<setw(12-dig_num(group[i].getID()))<<'\0'<<group[i].getTestScore(j);
else
cout<<setw(16-dig_num(group[i].getTestScore(j-1)))<<'\0'<<group[i].getTestScore(j);
}//end inner for
cout<<setw(16-dig_num(group[i].getTestScore(2)))<<'\0'<<group[i].getGrade()<<endl;
}//end outer for
}//end function print_info
int dig_num(unsigned int number){
int nd=0;
if(number==0)return 1;
while(number>0){
++nd;
number/=10;
}//end while
return nd;
}//end function dig_num
Student::Student(){
setFirstName("");
setLastName("");
for(int i=0;i<TEST_N;i++)
setTestScore(0,i);
setID(0);
setGrade('X');
}//end constructor
void Student::setFirstName(string FirstName){
firstName=FirstName;
}//end method setFirstName
string Student::getFirstName(){
return firstName;
}//end method getFirstName
void Student::setLastName(string LastName){
lastName=LastName;
}//end method setLastName
string Student::getLastName(){
return lastName;
}//end function getLastName
void Student::setTestScore(int Score,int Index){
test[Index]=Score;
}//end method setTestScore
int Student::getTestScore(int Index){
return test[Index];
}//end method getTestScore
void Student::setID(unsigned int ID){
id=ID;
}//end method setID
unsigned int Student::getID(){
return id;
}//end method getID
void Student::setGrade(char Grade){
grade=Grade;
}//end method setGrade
char Student::getGrade(){
return grade;
}//end method getGrade
|