ifstream myfile;//file reading mode
myfile.open("file2.txt");//file opened
if(!myfile)
{
cout<<"your file cannot be opened";
}
for(;!myfile.eof();)
{
myfile>>name>>salary>>concerned_department;
cout<<name<<"\t"<<salary<<"\t"<<concerned_department<<"\n";
}
do
{
cout<<"To search an employee please enter the name of the employee<<"\n";
cin>>empName;//will take the string from the user.
cout<<empName<<"\n";
ifstream myfile;//file reading mode
myfile.open("file2.txt");//file opened successfully
if(strcmp(name,"empName")==0)//here the main problem lies
{
myfile>>name>>salary>>concerned_department;
cout<<name<<"\t"<<salary<<"\t"<<concerned_department<<"\n";
}
else
{//
cout<<"ERROR "could not be compared"<<"\n";
}
cout<<"Do you want to continue (y/n)";
cin>>con;
}
while(con=='y');
Функция strcmp () не сравнивает строки, хотя дано. строка1 указана в файле, а строка2 взята у пользователя.
Используйте следующий код:
if(strcmp(name,empName)==0)
{
...
}
Обратите внимание, что там должно быть без кавычек вокруг empName
сравнить его содержание.
Других решений пока нет …