Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS
- #endif
- // INCLUDE:
- #include <iostream> // cin, cout
- #include <cstring> // strcpy, strlen, strncmp
- #include <cctype> // toupper
- #include <cstdlib> // itoa
- #include <iomanip> // setw
- using namespace std;
- // CONST:
- const int MAXEMP = 100; // Max. amount of employees.
- const int MAXTXT = 80; // Max lenght for text/string.
- const int DATELEN = 7; // Fixed lenght for date incl .
- // ENUM:
- enum Gender { female, male };
- // FUNCTION DECLARATION
- void printMenu();
- char read();
- int read(const char* t, int min, int max);
- void newEmployee();
- // KLASSER:
- class Person {
- protected:
- char* firstname; // Person's firstname.
- char dob[DATELEN]; // Date of birth format yymmdd
- public:
- Person() {
- cout << "nfirstname: ";
- cin.getline(firstname, MAXTXT); // This crashes the program after I've written in a firstname
- cout << "ndob (yymmdd): "; cin.getline(dob, DATELEN); // This seems to work
- }
- };
- class Grownup : public Person {
- protected:
- char* lastname; // Grownups' lastname
- public:
- Grownup() {
- cout << "Lastname: "; cin.getline(lastname, MAXTXT); // The program also crashes when entering lastname
- }
- };
- class Employee : public Grownup { // Employee class
- private:
- int nr; // Unique employee ID number
- public:
- Employee() {
- cout << "nnWARNING: This message should never be displayednn";
- }
- Employee(int n) {
- nr = n; // Sets the nr to whatever is sent in the parameter
- }
- };
- // GLOBAL VARIABLES
- Employee* employees[MAXEMP + 1]; // Array with pointers to all the employees
- int lastUsed; // Last used empolyee "array number"
- // MAIN PROGRAM
- int main() {
- char command; // Users wish/command
- printMenu(); // Prints menu with command options
- command = read(); // Read users command wish
- while (command != 'Q') {
- switch (command) {
- case 'N': newEmployee(); break; // Add a new employee
- default: printMenu(); break; // Print menu
- }
- command = read(); // Reads users command wish
- }
- return 0;
- }
- // FUNCTION DEFINITIONS
- void printMenu() {
- cout << "nnCOMMANDS AVAILABLE:";
- cout << "ntN - New employee";
- cout << "ntQ - Quit";
- }
- char read() { // reads and returns in uppercase
- char ch;
- cout << "nnCommand: ";
- cin >> ch; cin.ignore();
- return (toupper(ch));
- }
- // Reads leadtext (t), read and
- // return a number between min and max
- int read(const char* t, int min, int max) {
- int number;
- do {
- cout << 't' << t << " (" << min << '-' << max << "): ";
- cin >> number; cin.ignore();
- } while (number < min || number > max);
- return number;
- }
- void newEmployee() { // N - NEW EMPLOYEE:
- if (lastUsed <= MAXEMP) { // read employeenumber
- employees[++lastUsed] = new Employee(read("Employee nummer", 0, 9999));
- }
- else cout << "nMax amount of employees reached";
- }
Add Comment
Please, Sign In to add comment