我正在嘗試編寫一個程序,該程序從用戶處獲取測量值並將它們輸入到矢量中。 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