Question: The program in C++ below asks for name, salary, and ID . How can I restrict the ID will be entered by the user to

The program in C++ below asks for name, salary, and ID. How can I restrict the ID will be entered by the user to 4 digits only?

NOTE: I WANT TO KEEP ID TYPE AS INTEGER. I NEED THE SIMPLIEST SOLUTION WITH MINIMAL CHANGE WITHIN THE ENTIRE PROGRAM.

INCOMPLETE OR WRONG ANSWERS WILL GET NEGATIVE VOTES!

THANK YOU.

// PROGRAM STARTS HERE

#include using namespace std; class EMPLOYEE//class definition {//private data members private: string name; double salary; int ID; public:

//default constructor EMPLOYEE(): name(""), salary(0.0), ID(0){} //getter methods for private data members string getName() { return name; } double getSalary() { return salary; } int getID() { return ID; } //setter methods for fata members void setName(string n) { name = n; } void setSalary(double s) { salary = s; } void setID(int i) { ID = i; } }; int main() { EMPLOYEE e1;//create instance of employee string name; double salary; int id; //take input for data members cout << "Enter name of Employee: "; getline(cin,name); cout << "Enter salary of Employee: "; cin >> salary; cout << "Enter ID of Employee: "; cin >> id; //set values for data members e1.setID(id); e1.setName(name); e1.setSalary(salary);

//print details entered using getter methods printf("Details entered for the employee: ID = %d, Name: %s, Salary: %.2f", e1.getID(), e1.getName().c_str(), e1.getSalary()); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!