如果声明不起作用,即使声明是真的

AHF*_*AHF 0 c++

我的文本文件包含

Wew213
Wew214
Wew215
Run Code Online (Sandbox Code Playgroud)

我在程序中的输入是

Wew213
Run Code Online (Sandbox Code Playgroud)

但它告诉我输出

"Not Matched"
Run Code Online (Sandbox Code Playgroud)

实际上我正在做的是我想输入输入,如果输入匹配文本文件中的数字,它应该通过if语句运行输出否则语句

这是我的计划

char file_data[10];
std::ifstream file_read ("D:\\myfile.txt");
cout<<"Enter the number to search"<<endl;
char val[10];
cin>>val;
while(!file_read.eof())
{
    file_read>>file_data;
    cout<<file_data<<endl;
    }
    if (val == file_data)
    {
        cout<<"Matched"<<endl;
    }
    else
    {
           cout<<"Not Matched"<<endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

Bry*_*hen 8

你正在比较指针值,这是不同的

你需要用来strcmp比较c字符串.或使用std::string

if (strcmp(val, file_data) == 0)
{
    cout<<"Matched"<<endl;
}
else
{
       cout<<"Not Matched"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

要么

if (std::string(val) == std::string(file_data))
{
    cout<<"Matched"<<endl;
}
else
{
       cout<<"Not Matched"<<endl;
}
Run Code Online (Sandbox Code Playgroud)