Guest User

Untitled

a guest
Feb 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #ifdef _MSC_VER
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #endif
  4.  
  5. // INCLUDE:
  6. #include <iostream> // cin, cout
  7. #include <cstring> // strcpy, strlen, strncmp
  8. #include <cctype> // toupper
  9. #include <cstdlib> // itoa
  10. #include <iomanip> // setw
  11.  
  12. using namespace std;
  13.  
  14. // CONST:
  15. const int MAXEMP = 100; // Max. amount of employees.
  16. const int MAXTXT = 80; // Max lenght for text/string.
  17. const int DATELEN = 7; // Fixed lenght for date incl .
  18.  
  19. // ENUM:
  20. enum Gender { female, male };
  21.  
  22. // FUNCTION DECLARATION
  23. void printMenu();
  24. char read();
  25. int read(const char* t, int min, int max);
  26. void newEmployee();
  27.  
  28. // KLASSER:
  29. class Person {
  30. protected:
  31. char* firstname; // Person's firstname.
  32. char dob[DATELEN]; // Date of birth format yymmdd
  33.  
  34. public:
  35. Person() {
  36. cout << "nfirstname: ";
  37. cin.getline(firstname, MAXTXT); // This crashes the program after I've written in a firstname
  38. cout << "ndob (yymmdd): "; cin.getline(dob, DATELEN); // This seems to work
  39. }
  40. };
  41.  
  42. class Grownup : public Person {
  43. protected:
  44. char* lastname; // Grownups' lastname
  45.  
  46. public:
  47. Grownup() {
  48. cout << "Lastname: "; cin.getline(lastname, MAXTXT); // The program also crashes when entering lastname
  49. }
  50. };
  51.  
  52.  
  53.  
  54. class Employee : public Grownup { // Employee class
  55. private:
  56. int nr; // Unique employee ID number
  57.  
  58. public:
  59. Employee() {
  60. cout << "nnWARNING: This message should never be displayednn";
  61. }
  62.  
  63. Employee(int n) {
  64. nr = n; // Sets the nr to whatever is sent in the parameter
  65. }
  66.  
  67. };
  68.  
  69. // GLOBAL VARIABLES
  70. Employee* employees[MAXEMP + 1]; // Array with pointers to all the employees
  71. int lastUsed; // Last used empolyee "array number"
  72.  
  73. // MAIN PROGRAM
  74. int main() {
  75. char command; // Users wish/command
  76.  
  77. printMenu(); // Prints menu with command options
  78.  
  79. command = read(); // Read users command wish
  80. while (command != 'Q') {
  81. switch (command) {
  82. case 'N': newEmployee(); break; // Add a new employee
  83. default: printMenu(); break; // Print menu
  84. }
  85. command = read(); // Reads users command wish
  86. }
  87. return 0;
  88. }
  89.  
  90. // FUNCTION DEFINITIONS
  91. void printMenu() {
  92. cout << "nnCOMMANDS AVAILABLE:";
  93. cout << "ntN - New employee";
  94. cout << "ntQ - Quit";
  95. }
  96.  
  97.  
  98. char read() { // reads and returns in uppercase
  99. char ch;
  100. cout << "nnCommand: ";
  101. cin >> ch; cin.ignore();
  102. return (toupper(ch));
  103. }
  104. // Reads leadtext (t), read and
  105. // return a number between min and max
  106. int read(const char* t, int min, int max) {
  107. int number;
  108. do {
  109. cout << 't' << t << " (" << min << '-' << max << "): ";
  110. cin >> number; cin.ignore();
  111. } while (number < min || number > max);
  112. return number;
  113. }
  114.  
  115.  
  116. void newEmployee() { // N - NEW EMPLOYEE:
  117. if (lastUsed <= MAXEMP) { // read employeenumber
  118. employees[++lastUsed] = new Employee(read("Employee nummer", 0, 9999));
  119. }
  120. else cout << "nMax amount of employees reached";
  121. }
Add Comment
Please, Sign In to add comment