我需要帮助...如何获取此程序以读取人的字符串类型,并查看字符串是否相等?
#include <iostream>
using namespace std;
int main()
{
char name;
cout << "Type my name is:";
cin >> name;
if
name ==char('Mike') //this is where i think the problem is...
cout << "congrats";
else
cout << "Try again";
}
最佳答案
#include <iostream>
int main()
{
std::string name;
std::cout << "Type my name is:";
std::cin >> name;
if (name == "Mike") // Compare directly to the string "Mike"...
std::cout << "congrats";
else
std::cout << "Try again";
}
我认为使用
std::
而不是using namespace std
总是更好的习惯。关于c++ - 我如何获得该程序以读取人的字符串类型并查看字符串是否相等?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20916935/