I am here, including a simple program written in C ++, where I am trying to use a parameterized constructor. My idea is to dynamically create a class and complete the required task. But whenever I run the program and enter task 1, it just prints two lines (i.e. Enter Name.Enter Tel.No.). In fact, it is supposed to print "Enter Name". then enter the name, and then type "Enter Tel.No." again. How can I fix the problem? I have to use a parameterized constructor dynamically when creating an object.
#include <iostream> #include <conio.h> #include <fstream> #include <string> using namespace std; class myClass { string fullname,telephone; public: myClass(int taskType = 2) { if(taskType==1) { add_record(); } else if(taskType==2) { //view_records(); } else if(taskType==3) { //delete_record(); }else{ // custom_error(); } } void add_record() { cout << "Enter Name.\n"; getline(cin, fullname); cout << "Enter Tel. No.\n"; getline(cin, telephone); } }; main (){ int myTask; cout << "Enter a Task-Type. \n" << "1 = Add Record," << "2 = View Records," << "3 = Delete a Record\n\n"; cin >> myTask; myClass myObject(myTask); getch(); }
source share