0
While Loop Question (Or any for that matter...)C++
In C++, can you create a while loop or condition that checks the type of an input? For example: cout<<âWhat is 5-1?â; cin>>answer; While(answer != âtype intâ) { cout<<âPlease enter a valid input type.â; } Basically, is there a way to make the condition fail from inputting a character for example instead of an integer?
2 RĂ©ponses
+ 1
Ok, so you have not understood how cin works. I don't blame you, it is a bit confusing.
Basically, you can think of it like there are completely separate functions: cin<int>, which reads an integer, cin<char>, cin<string> and so on. When you call "cin >> answer", if answer is an int, it will call cin<int>, regardless of what the input is.
As far as cin is concerned, all input is of the same type: ASCII text. When you call cin<char> it just gives you the next character, while with cin<int>, it looks for consecutive digits, and then converts them into a binary representation the computer can work with.
0
Vlad Serbu
Ok, I get it from this perspective. Cin acts like (if it isnât one already) a pretty freelance, or global object with a unique type