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
|
//CS 540, Final Exam, Fadi AL Sheikh Aleais. Problem2.
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
class Student{
public:
Student(const string name, unsigned number);
string getName() const;
unsigned getNumber() const;
void setName(string name);
void setNumber(unsigned number);
private:
string myName;
unsigned myNumber;
}; // class Student
void die(const string & msg);
void showName(Student cs[] , unsigned els, unsigned number);
int main()
{
// storage for students
Student cs[] = { Student("Nick", 13 ), Student( "Chris", 10 ), Student( "Fadi", 8 ), Student( "Hely", 4 ), Student( "Brown", 3 ) };
showName(cs, 5, 3);
int x;
cin >> x;
return 0;
}//main
Student::Student(const string name, unsigned number) :myName(name), myNumber(number)
{
if (number > 10000 && number == 10000)
{
die("Unaccepted input.");
}
}
string Student::getName() const
{
return myName;
}
unsigned Student::getNumber() const
{
return myNumber;
}
void Student::setName(string name)
{
myName = name;
}
void Student::setNumber(unsigned number)
{
if (number > 10000 && number == 10000)
{
die("Unaccepted input.");
}
myNumber = number;
}
void showName(Student cs[], unsigned els, unsigned number)
{
try
{
// look at position - 1 because start index is 0
if(cs[els-1].getNumber() == number)
{
std::cout << cs[els-1].getName() << endl;
}
}
catch(std::exception &e)
{
// if error occurs
die("not found");
}
}
void die(const string & msg)
{
cerr << "Fatal error: " << msg << endl;
}
|