2014-01-10 4 views
-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) 
{ 
    ... 
} 

참고 :

+7

'name'을 문자열 리터럴''empName ''또는 변수'empName'과 비교한다는 의미입니까? 당신은 이전을하고 있습니다 .. –

+0

그것의 char 이름 [20] & empName [20]; – UsmanAhmad

+0

스택 오버플로에 오신 것을 환영합니다. 기꺼이 도와 드리겠습니다! 당신이 시도한 것에 대해 더 많은 설명을 해주시고 더 나은 결과를 도출 할 수 있도록 조치를 취하셨습니까? – Alejandro

답변

0

다음 코드를 사용합니다.

+0

이 작동하지 않습니다. 고맙습니다. – UsmanAhmad

관련 문제