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
//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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started