我正在尝试编写一个程序,该程序从用户那里获取测量值并将它们输入到向量中。while 循环一直持续到用户输入 '|' 此时它会跳出循环并打印测量值。然而,我遇到的问题是,当尝试将测量值添加到向量时。我使用了调试器,发现循环实际上从未进入 for 循环,因此无法到达“push_back 语句”。
该程序是 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();
}