c++ - Can you explain HOW bool can control loops? -
i'm beginner programmer in c++ (currently), , i've got conceptual question.
i'm trying filter cin input ensure one-or-two-digit integer between 01-04, , if isn't, produce error , ask new input.
i'm using map give user list of options that, upon valid selection, routes inputs (integers) through of several methods produce relevant result, i'll ask more specific version of question elsewhere.
i found snippet of code @ http://www.cplusplus.com/forum/beginner/26821/ meant validate input. sort of it, except boolean condition set inside while loop. because don't understand it, makes difficult edit or make sure i'm manipulating right.
here example code:
int main() { int num; bool valid = false; while (!valid) { valid = true; //assume cin integer. cout << "enter integer value: " << endl; cin >> num; if(cin.fail()) //cin.fail() checks see if value in cin //stream correct type, if not returns true, //false otherwise. { cin.clear(); //this corrects stream. cin.ignore(); //this skips left on stream data. cout << "please enter integer only." << endl; valid = false; //the cin not integer try again. } } cout << "you entered: " << num << endl; system("pause"); return 0; and here code (the entire thing, give context). don't think it's complete, want make sure i'm using boolean right.
float _tmain(float argc, _tchar* argv[]) { bool validinput = !true; map<string,int> operations; operations.insert(pair<string, int>("addition", 01)); operations.insert(pair<string, int>("subtraction", 02)); operations.insert(pair<string, int>("multiplication", 03)); operations.insert(pair<string, int>("division", 04)); cout << "welcome oneopcalc, operation perform?" << endl; for(map<string, int>::iterator ii=operations.begin(); ii!=operations.end(); ++ii) { cout << (*ii).second << ": " << (*ii).first << endl; } while (!validinput) { cin >> operatorselection; if (cin.fail() || operatorselection < 4 || operatorselection > 1) { cout << "error: invalid selection. please choose valid number." << endl << endl; cin.clear(); cin.ignore(); } } } does while (!valid) mean "while valid false"? in head, it's saying "while valid !valid", obviously, false.
edit: answers guys, i'm looking through them all. 1 answer keep getting goes general; understand ! not, , understand concept of flipping bool using it. implicit logical implications confuse me. in given statement, used thinking of !valid way of flipping valid value; not testing condition. it's syntax of using test condition tricks me. in other words, writing while(!valid) reads literally me while(notvalid), not while(valid==false). can't myself understand why in case, !valid reads condition , not bit-flip.
loops (and ifs) controled expression of type bool. in while ( !valid ), expression !valid, operator not applied value of variable valid. while ( !valid ) means (literally) while expression !valid (which means "not valid") true.
for rest, code you're copying pretty bad. wouldn't use example if you.
as own code:
_tmainparticular microsoft. don't want use it. if writing console application, usemain. (same thing holds_tchar, ratherchar.)neither
_tmainnormaincan returnfloat. return type ofmainshouldint. i'm less familiar_tmain, it's eitherintorvoid. (probablyint, if you're in console mode program.)!truefalse. always. (programming different real world. there no maybes.) why more complicated necessary?there's no need flag variable @ all. can write:
cin >> operatorselection; while ( !cin || operatorselection > 4 || operatorselection < 1 ) { // ... }
in case of error, ignore single character. want ignore , including end of line. (
std::cin.ignore( std::numeric_limits<std::streamsize>::max() );.and condition in if true. (see version above.) expect find number neither less nor greater one?
Comments
Post a Comment