我對編碼非常陌生,只是玩弄矢量,但我似乎無法找到如何在元素數量由用戶定義時一起添加矢量中的所有元素。如何將矢量中的所有元素一起添加? C++
#include <iostream>
#include <vector>
using namespace std;
int NoOfItems;
int i=1;
double Odds;
double Cost;
vector<double> CaseNumber;
int main()
{
cout << "How many items in the case: ";
cin >> NoOfItems;
while (true) {
if (NoOfItems == 0) {
break;
} else {
cout << "Odds for item " << i <<endl;
cin >> Odds;
CaseNumber.push_back(Odds);
NoOfItems = NoOfItems - 1;
i = i + 1;
}
}
}
'while(NoOfItems> 0){...}' –
爲什麼你的所有變量都是glob人? –