我正在尝试编写一个程序,该程序从用户处获取测量值并将它们输入到矢量中。 while循环继续,直到用户输入'|'在此时它会跳出循环并打印测量结果。然而,我遇到的问题是,当试图将测量结果添加到矢量中时。我使用了调试器,发现循环从未实际进入for循环,因此无法达到“push_back语句”。While循环内的C++ For循环
该程序是Bjarne Stroustup PPP C++书籍的一部分。
#include "../../std_lib_facilities.h"
double metreConvert (double userInput , String unit) {
if (unit == "cm")
userInput = userInput/100;
else if (unit == "in")
userInput = (userInput * 2.54)/100;
else if (unit == "ft")
userInput = ((userInput * 12) * 2.54)/100;
else if (unit == "m")
userInput;
return userInput;
}
void numbers() {
double input;
String unit;
vector <double> measurements;
while (cin >> input >> unit && input != '|'){
if (unit == "cm")
input = input/100;
else if (unit == "in")
input = (input * 2.54)/100;
else if (unit == "ft")
input = ((input * 12) * 2.54)/100;
else if (unit == "m")
input;
for (int i=0; measurements.size(); i++){
if (i == 0 || input != measurements[i]){
cout << "\nPopping onto vector";
measurements.push_back(input);
}
else {
cout << "\nMeasurment cannot be placed on vector";
}
}
}
cout << "input ended";
}
void main() {
cout << "Please enter a number followed by a unit(metres,cm,inches,ft), type '|' when finished inputing:";
numbers();
}
请正确缩进你的代码。另外,'for'循环之前的'input'是什么? –
measurements.size() - 也许检查不是0? – Jona