I'm in an intro to C++ class and I was wondering of a better method of checking if input was the desired type.
Is this a good way of doing this? I come from a PHP/PERL background which makes me rather apprehensive of using while loops.
char type;
while (true) {
cout << "Were you admitted? [y/n]" << endl;
cin >> type;
if ((type == 'y') || (type == 'n')) {
break;
}
}
Is this a safe way of doing this or am I opening myself up to a world of hurt, which I suspect? What would be a better way of making sure I get the input I want before continuing?
while
loops... – Whippleforeach
and the like replaced many uses offor
andwhile
in dynamic languages. that has had the nice effect of squashing many bugs in the terminating logic, and the ugly effect that people now feel insecure when they need awhile
. but that's only once in a while, for sure! – Ahasuerusforeach
replacement forwhile(type != 'n' && type != 'y')
– Whippleforeach
[...] replaced many uses offor
andwhile
", notice many as opposed to all. My claim that "poeple now feel insecure when they need awhile
[statement]" should also make it clear thatwhile
is still needed. But maybe I'm just conflating dynamic languages with "enterprise" (web) applications where vast majority of loops are over collections such as db resultsets. For example a banking application I've been working on contains 80while
loops and 1590foreach
loops. – Ahasuerus