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 ответов
+ 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