如果统计语句不起作用
If stat statement not working
我试图根据给出的用户输入让 if 语句为真或假,但是当我输入条件时,它说这对二进制表达式的操作数无效 ("字符串"(又名"basic_string")和"int")
这是我的代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl;
cout << " X X" << endl;
cout << " X MENU X" << endl;
cout << " X X" << endl;
cout << " X Press 1 to Play The Word Game. X" << endl;
cout << " X X" << endl;
cout << " X Press 2 Too See a calculator. X" << endl;
cout << " X X" << endl;
cout << " X X" << endl;
cout << " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << endl
<< endl;
string userinput;
cin >> userinput;
if (userinput == 1);{
while (1<2)
{
cout << "Enter the Word." << endl;
string UserInput;
cin >> UserInput;
cout << endl << UserInput << " is is not the right word but keep on trying" << endl << endl;
cout << "Press Enter to Continue.";
cin.clear();
cin.ignore(255, 'n');
cin.get();
return 0;
}
}
if (userinput == 2);
{ int z;
int x;
int sum;
cout << " welcome to the calculator" << endl << endl;
cin.clear();
cin.ignore(255, 'n');
cin.get();
cout << "enter your first number";
cin >> z; cout << endl << endl;
cout << "enter your second number" << endl;
cin >> x; cout << endl << endl;
sum = z + x;
cout << sum;
}
cin.clear();
cin.ignore(255, 'n');
cin.get();
return 0;
}
if (userinput == 2);
^^^
同样,if
之后到处都有分号,我认为这不是您想要的。
删除此处的分号:
if (userinput == 1);{
if (userinput == 2);
而且您无法将字符串与整数进行比较。
string userinput;
cin >> userinput;
if (userinput == 1);{
所以试试
int userInput ;
如果您坚持使用字符串,请查看:http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
相关文章:
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 默认标签显然在 switch 语句中不起作用
- C++ 中断语句不起作用
- 我的代码应该接受一个数字,并返回字母等级或"Grade is not valid"但 else 语句不起作用
- 使用 cin、if 语句和数组不起作用
- (C++) IF 语句似乎根本不起作用
- 为什么当我输入字符时,这个特定的 cin.get() 语句在C++中不起作用?
- QTranslator 在 if 语句中使用时不起作用
- 我的开关语句在C++中不起作用
- 切换语句不起作用 c++ 和 D:\c++ dev-c\Makefile.win 目标"abc.o"的配方失败
- C++ 默认值在开关语句中不起作用
- C++ SQLite3 准备的删除语句不起作用
- 如果语句在我的代码中不起作用
- 一个'case'在简单的'switch'语句中不起作用
- 我的程序在SCANF语句之后不起作用
- std::cin 语句不起作用 C++
- 为什么我的 if 语句不起作用
- C++ 递增在 If else 语句中不起作用
- if 语句在 for 循环中不起作用
- 我的 for 循环之后的代码不起作用,for 循环后的打印语句不会被打印