我正在尝试从字符串中提取整数,例如用户输入可能是5ft12in
或 5'12"`。
但是,当我的输入为 时,代码有效5ft1in
,但在输入时无效5ft12in
。
我想遍历整个字符串并提取 3 个数字,如下所示:
feet = 5
inches 1 =1
inches 2 = 2
但我似乎无法找到问题所在。
我认为有一种方法可以将输入转换为stringstream
然后peek
使用整个字符串char
,但我不太确定如何。
string feet = "";
string inches = "";
string inches2 = "";
for (int i = 0; i < height.size(); ++i) {
if (isdigit(height[i]) && (feet.empty())) {
feet = height[i];
} // end if
else if ((isdigit(height[i]) && (!feet.empty()))) {
inches = height[i];
}
else if ((isdigit(height[i]) && (!feet.empty() && (!inches.empty())))) {
inches2 = height[i];
} // end else if
}//end for
cout << "String of feet : " << feet << endl;
cout << "String of inches 1 : " << inches << endl;
cout << "String of inches 2 : " << inches2 << endl;