C++ 检查数据是否相等

c++ checking to see if data is equal or not

本文关键字:是否 数据 检查 C++      更新时间:2023-10-16

我正在编写一个程序,必须检查用户输入的字符是否等于数组中的一个字符。如果它不等于任何,则应显示"无效...."。它对我不起作用,任何人都可以向我解释我做错了什么。我每次都得到无效的字符。

我有一个字符数组数据[5];存储5个字母

 cout<<"Enter one character to delete: ";
 cin>>del;

 cout<<del;
 for(int x=0;x<4;x++)
    {
      if(del!=data[x])
        {
         cout<<"Invalid, character not entered.n";
         break;
         }
    }
for(int x=0;x<5;x++)
{
  if(del==data[x])
    {
     cout<<"Character found at " << x << endl;
     break;
     }
} if(x==5) cout<<"Character not found" << endl;

如果你的字符数组是 C 样式的以 null 结尾的字符串,那么你可以使用 strchr:

#include <cstring>
//...
if (strchr(data, del)) {
    // character found
}
else {
    // character not found
}