2014-01-10 103 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從用戶處獲取。Strcmp()函數無法正常工作

+7

你的意思是將'name'與字符串文字'empName''或變量'empName'進行比較嗎?你正在做前... –

+0

其char名稱[20]&empName [20]; – UsmanAhmad

+0

歡迎來到Stack Overflow,我們很樂意幫助你!您能否提供更多的解釋,說明您嘗試過的方法以及您嘗試解決問題的步驟,以便我們能夠更好地幫助您? – Alejandro

回答

0

使用下面的代碼:

if(strcmp(name,empName)==0) 
{ 
    ... 
} 

需要注意的是,必須有沒有引號各地empName來比較其內容。

+0

它不能正常工作。雖然Thanx回覆 – UsmanAhmad