conditions for multiple variables in while loop c++ -
im trying while loop show "invalid input" while loop 3 variables. im starting out c++ im not best @ trying show need here in terms.
while(children1,children2,children3)<1||(children1,children2,children3)>100) cout << "invalid input"; return 0;
it's easiest to express each requirement separately:
while (children1 < 1 || children2 < 1 || children3 < 1 || children1 > 100 || children2 > 100 || children3 > 100) std::cout << "invalid input\n";
a c++11 alternatives is:
#include <algorithm> ... while (std::min({ children1, children2, children3 }) < 1 || std::max({ children1, children2, children3 }) > 100) std::cout << "invalid input\n";
fwiw, if kept numbers in std::vector
- arguably more elegant - option available:
std::vector<int> children; children.push_back(23); children.push_back(123); children.push_back(13); // ...however many like... while (std::any_of(std::begin(children), std::end(children), [](int n) { return n < 1 || n > 100; })) std::cout << "invalid input\n";
note if intend multiple instructions controlled statement while
, if
, or for
, need group them using braces:
int retries_available = 3; while (std::any_of(std::begin(children), std::end(children), [](int n) { return n < 1 || n > 100; })) { std::cout << "invalid input\n"; if (retries_available-- == 0) return 0; get_new_values_for(children); }
Comments
Post a Comment