-6
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() 함수는 주어진 문자열을 비교하지 않습니다. string1은 파일에서 제공되는 반면 string2는 사용자로부터 가져옵니다. 그 내용을 비교하기 empName
주위 에는 인용 부호가 없어야한다는Strcmp() 함수가 제대로 작동하지 않습니다.
if(strcmp(name,empName)==0)
{
...
}
참고 :
'name'을 문자열 리터럴''empName ''또는 변수'empName'과 비교한다는 의미입니까? 당신은 이전을하고 있습니다 .. –
그것의 char 이름 [20] & empName [20]; – UsmanAhmad
스택 오버플로에 오신 것을 환영합니다. 기꺼이 도와 드리겠습니다! 당신이 시도한 것에 대해 더 많은 설명을 해주시고 더 나은 결과를 도출 할 수 있도록 조치를 취하셨습니까? – Alejandro