gam*_*l22 0 c++ multidimensional-array
我正在读取数据到结构数组.数据由可能的空行分隔,我必须忽略.我的代码不工作我想知道为什么
struct Location
{
string state;
string city;
int zipcode;
}
Run Code Online (Sandbox Code Playgroud)
并且继续阅读我的麻烦.
while (!fin.eof() && size < 50)
{
getline (fin, location[size].state);
getline (fin, location[size].city);
fin >> location[size].zipcode;
if (location[size].empty()) //to ignore blank lines but its not working?
continue;
size++;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?它可能是编译器吗?
看起来你正在尝试检查一个空字符串但无意中试图调用empty()一个Location.
你的意思是
if (location[size].state.empty() && location[size].city.empty())
continue;
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您希望您的代码示例按原样工作,并且您可以修改struct Loaction,则可以执行以下操作.
struct Location
{
std::string state;
std::string city;
int zipcode; //who cares about zip+4
Location():zipcode(0){};
bool empty()
{
return state.empty() && city.empty() && !zipcode;
}
};
Run Code Online (Sandbox Code Playgroud)