Answer to Question #211439 in C++ for Hemambar

Question #211439

Create a class called Employee that contains the Name and Employee number (type long).

Include a member function called getData() to get data from the user for insertion into the object,

another function called putData() to display the data. The name should have no embedded blanks.

Write a main() program to exercise this class. It should create array of type Employee and then

invite the user to input data for up to 10 employees. Finally, it should print out the data for all the

employees. (Use setw, setiosflags to format the output).


1
Expert's answer
2021-06-28T07:24:45-0400
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


class Employee {
private: 
    string name;
    long number;
public:
    void getData();
    void putData();
};


void Employee::getData() {
    cout << "Enter a name of an employee: ";
    cin >> name;
    cout << "Enter the emplyee number: ";
    cin >> number;
}


void Employee::putData() {
    cout << setw(20) << setiosflags(cout.left) << name << ":" 
         << setw(10) << setiosflags(cout.right) << number;
}


int main() {
    Employee employees[10];
    int n=0;


    for (n=0; n<10; n++) {
        cout << "Data to the " << n+1 << "th employee" << endl;
        employees[n].getData();


        char ans;
        cout << "Enter another data (y/n)? ";
        cin >> ans;
        if (ans != 'y' && ans != 'Y')
            break;
    }
    if (n!=10)
        n++;
    
    cout << setw(20) << setiosflags(cout.left) << "Name" << ":"
         << setw(10) << setiosflags(cout.right) << "Number" << endl;
    cout << "--------------------:----------" << endl;
    for (int i=0; i<n; i++) {
        employees[i].putData();
        cout << endl;
    }
    cout << endl;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS