C++ 检查数据是否相等
c++ checking to see if data is equal or not
我正在编写一个程序,必须检查用户输入的字符是否等于数组中的一个字符。如果它不等于任何,则应显示"无效...."。它对我不起作用,任何人都可以向我解释我做错了什么。我每次都得到无效的字符。
我有一个字符数组数据[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
}
相关文章:
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- C++ 11 中的锁定是否保证访问数据的新鲜度?
- 是否可以根据其数据成员的类型确定类型的大小
- 检查串行端口Linux中是否有传入数据(cbInQue for linux)
- 检查输入 std::array 指针数据是否等于某个常量数组
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- 是否可以使用右值初始化数据成员?
- 类对象在 c++ 中是否具有数据类型?
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- C++中是否有用于长数据类型的 lower_bound() 函数?
- std::weak_ptr 和相应的 std::shared_ptr 之间是否存在数据竞争?
- 将数据放在私有类中是否理想?(C++)
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- 从 std::string 到 std::array<char,size> 的 memcopy 额外数据是否是一种未定义的行为?
- 是否可以跨多个源文件构建 constexpr 数据结构?
- 即使类不包含数据,C++ 14 是否仍会生成默认函数?
- 如果我在块中编写字符串文字,是否会从数据部分复制整个字符串数据?
- C++,在对象内分配多个数据时,堆栈分配是否更有效? 在下面的程序中,类A_Heap的效率会更低吗?
- C++ - 检查结构数据类型中的单词是否为回文
- 在 C++17 中,是否未定义使用无锁原子学保护从信号处理程序传递的数据?