检查单词功能



我一周前开始学习c++,我需要一个关于如何检查没有check_pass函数输入的单词的建议。我怎么能使用它,如果或而功能,请帮助。(抱歉有些错误)

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int enter_word;

cout<<"Hey bro what is your favourite color?  ";
cin>>enter_word;
cout<<"So what is my favourite color?  ";
if (enter_word="yellow"){ cout<<"Yep you are right bro!";}
system("pause");
return 0;
}    

您显示的代码中有两个主要错误:首先是enter_word不是std::string对象,它是一个整数变量,因此只能包含整数。其次,您不比较 enter_word"yellow"的条件,您分配给变量。

第一个问题通过包含<string>并将enter_word声明为字符串解决:
std::string enter_word;
第二个问题可以通过使用相等比较操作符==而不是赋值来解决:
enter_word == "yellow"

最新更新