when i run my code (https://ide.codingblocks.com/s/73157)
with cin statement it doesnt give output but it work with cin.get()
and its regarding shortest road trip
Doubt regarding cin.get() and cin
HI @taran9873, cin >> is incapable of reading whitespaces where cin.get() can. You have set the break condition of your loop as while( ch!=’\n’ ) But if u are using cin>> it will never read a ‘\n’ as input and hence your loop will never break. However, the cin >> and cin.get() are doing same work for the characters before expected ‘\n’ . They both read N, S, E, W differently. So inorder to use cin>> you have to write a different condition in while loop. or u can cin the complete input as one string using cin>> and then interate on it using a for loop. Many methods are available choice is urs.
So finally here cin>> was not working because it was incapable to assign ch the value ‘\n’ which was required to break the while loop and hence it became an infinite loop.
Hope this helps
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.