Strcmp()函数无法正常工作

时间:2014-01-10 03:31:34

标签: c++

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则取自用户。

1 个答案:

答案 0 :(得分:0)

使用以下代码:

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

请注意,empName周围必须有无引号才能比较其内容。