【发布时间】:2013-04-17 03:00:59
【问题描述】:
编写一个简单的代码并遇到了一个我不知道如何处理的问题。我尝试通过搜索对其进行调查,但没有发现任何帮助,而且每个人的答案都超出了我的想象。请有人像对小孩一样解释这个,哈哈。谢谢。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string invCode = "";
string lastTwoChars = "";
cout << "Use this program to determine color of furniture.";
cout << "Enter five-character inventory code: ";
cin >> invCode;
if (invCode.length() == 5)
{
lastTwoChars = invCode.substr(3,2);
if (lastTwoChars == 41)
{
cout << "Red";
}
if (lastTwoChars == 25)
{
cout << "Black";
}
if (lastTwoChars == 30)
{
cout << "Green";
}
}
else
cout << "Invalid inventory code." << endl;
system("pause");
return 0;
}
【问题讨论】:
-
错误指向哪一行?
-
表示没有为字符串和整数的比较定义
==,lastTwoChars == 25无效。lastTwoChars == "25"就是你要找的。span> -
错误应该更具体...类似“运算符==(字符串,int)不匹配”之类的...阅读全文时更容易判断发生了什么事物。 :P
标签: c++ string operator-keyword