-1

I'm having trouble wrapping my head around this:

Can anyone tell me what this does? (Ps: this is my 3rd day of coding and I really want to understand this example)

https://i.stack.imgur.com/mpchI.png

if (std::cin >> currVal){
while (std::cin >> val){

walnut
  • 21,629
  • 4
  • 23
  • 59
  • I'm following a beginner's textbook and this was in one of their examples. I tried reading their explanation but I didn't quite understand it. – Michael Karibi Feb 07 '20 at 07:47
  • I have added an image of the example in the textook. From what I have learnt cin >> is a way of getting the user to input something. I don't understand how this can be used in a if statement or while loop. Because for if and while loops they need a comparison like =! or >= or ==. – Michael Karibi Feb 07 '20 at 08:09
  • @ThomasSablik I've read that and still don't quite understand. – Michael Karibi Feb 07 '20 at 08:13
  • See also [While Function and cin in C++](https://stackoverflow.com/questions/21633606/while-function-and-cin-in-c) and [C++ : cin inside a while loop](https://stackoverflow.com/questions/42038373/c-cin-inside-a-while-loop) and [c++ when will while(cin>>s) stop](https://stackoverflow.com/questions/50999994/c-when-will-whilecins-stop) – walnut Feb 07 '20 at 08:23
  • I've added a more detailed answer to the duplicate, I hope it helps: https://stackoverflow.com/questions/6791520/if-cin-x-why-can-you-use-that-condition/60109776#60109776 – Alan Birtles Feb 07 '20 at 08:37

1 Answers1

0

It pretty much sais: pipe in the value of number into Cin. Which is C's inputstream. This if-condition will fail if number is not a number and will evaluate to false.

Your syntax is incorrect. What you meant to do was if ( cin >> number).

In the comments, a question regarding strings arose:

Every input is a string. If you want to check if the string you entered is a number you can simply to convert it to one. You have to cin a string, and then convert it using eg. strol

An alternative could be to check if the reading from cin failed, but i generally don't use this approach because cin.fail() covers more error situations than just a failed type conversion.

You can read more here about cin here

  • What if `number` is a string? The answer is not complete. You didn't answer why you can use it. You only answered what will happen. – Thomas Sablik Feb 07 '20 at 07:51
  • @ThomasSablik There you go sir. –  Feb 07 '20 at 07:56
  • Now it's even worse. How can the input stream return something that's not a string? You should at least mention the return type of `operator>>`, the failbits and how a stream is casted to bool. – Thomas Sablik Feb 07 '20 at 07:57
  • @walnut it kinda does - `This if-condition will fail if number is not a number and will evaluate to false.` –  Feb 07 '20 at 08:05
  • @walnut that wasn't his question. And, I can read between the lines... –  Feb 07 '20 at 08:07
  • @user1234213211 OP has clarified the question now. So my stated issue is resolved. But before, it wasn't clear whether OP's textbook wasn't just wrong. – walnut Feb 07 '20 at 08:12
  • 1
    The question is why you can use the operator<< in the if condition. What is the returned value and how is it casted to bool – Thomas Sablik Feb 07 '20 at 08:14
  • @ThomasSablik That is answered.... –  Feb 07 '20 at 08:16
  • I don't see it in your answer. What exactly is returned? – Thomas Sablik Feb 07 '20 at 08:53