Advertisement
damoncard

C++ Exmaple

Sep 14th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Month {
  9.     public :
  10.         Month();
  11.         void setName(istream& name);
  12.         void setDays(istream& days);
  13.         void setHolidays(string* holidays);
  14.         string getName();
  15.         unsigned int getDays();
  16.         string* getHolidays();
  17.  
  18.     private :
  19.         string name;
  20.         unsigned int days;
  21.         string* holidays;
  22. };
  23.  
  24. void addDetail(Month& m) {
  25.     cout << "Input the name: ";
  26.     m.setName(cin);
  27.     cout << "Input the total day: ";
  28.     m.setDays(cin);
  29.     cout << "How many holiday: ";
  30.     int num;
  31.     cin >> num;
  32.     string* holidays = new string[num];
  33.     for (int f = 0 ; f < num ; f++) {
  34.         cout << "Name of " << f+1 << " holiday: ";
  35.         string h;
  36.         cin >> h;
  37.         holidays[f] = h;
  38.     }
  39.     m.setHolidays(holidays);
  40.     cout << "------------------------" << endl;
  41. }
  42.  
  43. void printDetail(Month m) {
  44.     cout << "Name: " << m.getName() << "\tDays: " << m.getDays() << "\tHolidays: " << m.getHolidays()[0];
  45.     for (int q = 1 ; q < sizeof(m.getHolidays()) / sizeof(m.getHolidays()[1]) ; q++) {
  46.         cout << ", " << m.getHolidays()[q];
  47.     }
  48.     cout << endl;
  49. }
  50.  
  51. int main() {
  52.     // Initialize Months
  53.     vector<Month> month;
  54.  
  55.     // Ask for input
  56.     char c;
  57.     while (c != 'q') {
  58.         Month m;
  59.         addDetail(m);
  60.         month.push_back(m);
  61.         cout << "Add(a) or Exit(q): ";
  62.         cin >> c;
  63.     }
  64.  
  65.  
  66.  
  67.     //Ask for output
  68.     cout << "Do you want to see output? (Y/n): ";
  69.     cin >> c;
  70.     if (c == 'Y') {
  71.         //Output the detail of each month
  72.         for_each(month.begin(), month.end(), printDetail);
  73.     }
  74.     return 0;
  75. }
  76.  
  77. Month::Month() {
  78.     name = "";
  79.     days = 0;
  80.     holidays = NULL;
  81. }
  82.  
  83. void Month::setName(istream& name) {
  84.     name >> this->name;
  85. }
  86.  
  87. void Month::setDays(istream& days) {
  88.     days >> this->days;
  89. }
  90.  
  91. void Month::setHolidays(string* holidays) {
  92.     this->holidays = holidays;
  93. }
  94.  
  95. string Month::getName() {
  96.     return this->name;
  97. }
  98.  
  99. unsigned int Month::getDays() {
  100.     return this->days;
  101. }
  102.  
  103. string* Month::getHolidays() {
  104.     return this->holidays;
  105. }
  106.  
  107. // Author : Sirawat Ngarmphandisorn
  108. // ID : 57130500220
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement