2014-01-10 60 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