C++ continuing past the while loop -
i'm having bit of trouble finding way bypass infinite input loop i've put myself in. here's code:
int main() { vector<double>v; double input; double low = numeric_limits<double>::max(); double high = numeric_limits<double>::min(); cout << "enter doubles in sequence. << '\n'; while (cin >> input) { v.push_back(input); if (input < low) { low = input; cout << low << "is new low" << '\n'; } if (input > high) { high = input; cout << high << "is new high" << '\n'; } sort(v.begin(), v.end()); (int = 0; < v.size(); ++i) { cout << v[i] << " "; } cout << '\n'; } cout << "test"; }
the last output me attempting test whether relay "test" me after attempting use break/continue. code wrote used make note of numbers person input , update whether lowest or highest number , sort vector. wanted program bypass showing work putting string outputs after while loop wouldn't update in real time whenever higher number or lower number entered.
for clarity issues, entered 2.6, 3.5, 6, 1.2, 9.2. update 2.6 highest far, 3.5, 6, etc. i'd process skipped , program show, @ end, 9.2 highest number , 1.2 lowest. thank assistance can give me.
since not want update highest , lowest values every time input given, can try following code:
while (cin >> input) { if(input==-1) break; v.push_back(input); } sort(v.begin(), v.end()); (int = 0; < v.size(); ++i) cout << v[i] << " "; cout<<endl; cout<<v[0]<<" lowest number."<<endl; cout<<v[v.size()-1]<<" highest number."<<endl;
so, main idea use sorting , printing outside while loop. should use while loop push inputs vector, remember use break condition this.
Comments
Post a Comment