在按行读入字符串并转为数字时报错:
while (getline(cin,str))
{
if (str.size() == 0) break;
istringstream iss(str);
while (iss >> num)
{
int score=atoi(num);
}
}
error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
原因:atoi接受const char*型,而参数为std::string型
解决方法:改为
int score=atoi(num.c_str());