在c++中,不等于(!=)运算符不能正确使用字符
Not equal (!=) operator not working with correctly char in c++
此代码工作不正常,请帮助我。即使我输入了正确的字符,它也一直要求"输入正确的数字"。它并没有评估这种情况。
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int main()
{
char ch = '0';
A:
cout << "enter a Character" << endl;
cin >> ch;
if ((ch != 'X')||(ch != 'x'))
{
cout << "Please Enter Right Number" << endl;
goto A;
}
return 0;
}
使用
if ((ch != 'X') && (ch != 'x'))
而不是
if ((ch != 'X')||(ch != 'x'))
也可以使用循环而不是使用goto
cout << "enter a Character" << endl;
cin >> ch;
while(ch!='X' && ch!='x')
{
cout << "Please Enter Right Number" << endl;
cout << "enter a Character" << endl;
cin>>ch;
}
(ch != 'X')||(ch != 'x')
始终是true
,您的意思可能是&&
而不是||
。
相关文章:
- 取消引用运算符不能重载
- 为什么文本运算符不能正常模板化?
- 不能将重载比较运算符与 Catch 测试一起使用
- 运算符'<'不能应用于类型 'string' 和 'string' 的操作数
- 为什么我不能在运算符=中使用引用类型?
- 为什么我在声明对象数组时不能使用 -> 运算符?
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- C++自定义删除运算符不能正常工作?
- 为什么我不能将运算符+与reverse_iterator一起使用?
- 为什么我们甚至需要删除运算符?(我们不能只使用删除[]吗)
- 为什么我不能用两个参数重载 C++ 运算符 []?
- ostream_iterator运算符=在pair<int,int>上失败,但在包装器类上工作。我不能为成对<>重载运算符<<吗?
- 在C++中,为什么int可以使用new运算符初始化变量,而double不能
- 为什么我不能使用在 Visual C++ 32 位中实现运算符无符号 int() 作为数组索引的类?
- 为什么我不能重载运算符<< 与 ostream 和 map( 或 unordered_map)?
- 为什么我不能定义一元运算符,然后在 MSVC 的模板类中声明具有相同名称的友元二进制运算符?
- C++:不能使运算符<<成为模板嵌套类的好友
- 不能在友元函数中使用重载运算符
- 为什么 test2 + test3 的运算符<<不能过载?
- 为什么不能运算符!=进行转换?