In this menu driven program, when I run the code and take "Add employee",
I am unable to read names from the user which has spaces.
I can read "John".
Console skips next cin if I enter something like "John Blake"
I tried cin.getline, fgets()
Nothing seem to work with this code.
cin.getline() works well if i try it in a new .cpp file, but not in this code
Please have a look
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
using namespace std;
class EmpData
{
public:
string det[6];
};
int showMenu()
{
int choice;
system("CLS");
cout<<"Welcome to Employee Management Portal \n_____________________________________\n";
cout<<"1.View all Employee details\n";
cout<<"2.Search for an employee\n";
cout<<"3.Add Employee\n";
cout<<"4.Delete Employee Record\n";
cout<<"5.Exit\n\n";
cout<<"Please select an option : ";
cin>>choice;
return choice;
}
EmpData obj[120];
int j=0;
void readFile()
{
string s;
ifstream myfile ("empdata.txt");
while(!myfile.eof())
{
getline (myfile,s);
istringstream ss( s );
for(int i=0;i<6;i++)
{
getline( ss, obj[j].det[i], '|' );
}
j++;
}
}
void dispDetails()
{
for(int i=0;i<j;i++)
{
for(int k=0;k<6;k++)
{
cout<<obj[i].det[k]<<"|";
}
cout<<"\n";
}
}
void empSearch()
{
string key;
cout<<"\nEnter the employee ID : ";
cin>>key;
int flag =0;
//cout<<"Sl|Emp ID| Employee Name | Department |Band|Location\n";
for(int i=0;i<j;i++)
{
for(int k=0;k<6;k++)
{
if(obj[i].det[1]==key)
{
cout<<obj[i].det[k]<<"|";
flag=1;
}
}
}
if (flag == 0)
{
cout<<"\nEmployee Details Not found";
}
}
void addEmp()
{
string s[5];
char name[30];
cout<<j;
cout<<"\nEnter the employee details to be added \nEmpID : ";
cin>>s[0];
cout<<"Name : ";
cin>>s[1];
cout<<"Role : ";
cin>>s[2];
cout<<"Band : ";
cin>>s[3];
cout<<"Location : ";
cin>>s[4];
std::ostringstream ostr;
ostr << j;
std::string theNumberString = ostr.str();
obj[j].det[0]=theNumberString;
for(int k=1;k<6;k++)
{
obj[j].det[k]=s[k-1];
}
j=j+1;
}
int main () {
char ch;
readFile();
do
{
int choice;
choice=showMenu();
switch(choice)
{
case 1:
cout<<"Emplyee Records\n";
//cout<<"Sl|Emp ID| Employee Name | Department |Band|Location";
dispDetails();
break;
case 2:
empSearch();
break;
case 3:
addEmp();
break;
case 4:
cout<<"Function to delete goes here";
break;
case 5:
exit(0);
default:
cout<<"Wrong choice...";
}
cout<<"\nEnter any key and press enter to continue) : ";
cin>>ch;
}
while(ch);
return 0;
}
|
file "empdata.txt" has following data
Sl|EmployeeID|Name|Department|Band|Location
1|327427|Brock Mcneil|Research and Development|U2|Pune
2|310456|Acton Golden|Advertising|P3|Hyderabad
3|305540|Hollee Camacho|Payroll|U3|Bangalore
4|218801|Simone Myers|Public Relations|U3|Pune
5|144051|Eaton Benson|Advertising|P1|Chennai
6|343657|Hilda Wooten|Legal Department|P4|Mumbai