suppose when i take user input through cin>>s and give the input in the console like (i eat mangoes) then when i write the functions like length() and iterate over the string it gives the output as 1 and i respectively .
Secondly when i default input some string (suppose the same string i took above) it gives the correct length and character .
Ex:- string s=“i eat mangoes” as a default input taken and now when i use length function it gives correct length.
This means when i take user input it is ending the search at first space it gets after “i” but when i take default input the same string it does not stop at first space .
WHY SO ???
Doubt when i take user input when i provide default input
There are 2 ways to input a string -
cin.get(s) and cin.getline(s)
cin.get(s) does not include spaces in the entered string.
But cin.getline(s) can be used to input spaces as well.
For example, if the entered string is - “how are you”
If you used cin.get(s), s will store only “how”. But in case of cin.getline(s), s will contain the entire string.
And cin>>s works line cin.get(s) only.
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.
1 Like