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)
if (std::cin >> currVal){
while (std::cin >> val){
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)
if (std::cin >> currVal){
while (std::cin >> val){
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