A bit new to C++ here. Is it possible to do something like the following?
int temp;
while(cin >> temp != -9999){//Do something with temp}
I can't get that exact code to work, but I feel like something such as that should be possible.
Edit Tried the following as well:
while(cin.getline(temp) != -9999){//Do something with temp}
Still nothing. Does getline() only work with strings?
Yes, it does:
while (std::cin >> temp && temp != -9999)
However, operator precedence in C++ is annoying, so I would use:
while (std::cin >> temp) {
if (temp == -9999)
break;
The reasoning is that std::cin is a stream. As such, reading from it returns the stream so you can do things like:
std::cin >> temp >> temp2;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With