why we have used cin.get and cin.getline different times if one is already used because i think both have same meaning.
Difference b/w cin.get and cin.getline
HI @jindaldivish
cin.getline() reads input up to ‘\n’ and stops
cin.get() reads input up to ‘\n’ and keeps ‘\n’ in the stream
For example :
char str1[100];
char str2[100];
cin.getline(str1 , 100);
cin.get(str2 , 100);
cout << str1 << " "<<str2;
input :
1 2
3 4
output 1 2 3 4 // the output expexted
When reverse them
For example :
char str1[100];
char str2[100];
cin.get(str2 , 100);
cin.getline(str1 , 100);
cout << str1 << " "<<str2;
input :
1 2
3 4
output 1 2 // the output unexpexted because cin.getline() read the ‘\n’
in your reply i didnt understand much that how can we enter integers as input despite character datatype.
can you explain it in much clear way the difference between cin.get and cin.getline used in the video lecture.
@jindaldivish 1, 2, 3, 4 are integers but they can be used as characters as well, they have the ascii value from 48(0) to 57(9). Refer to this table http://www.asciitable.com/ to clarify what all characters are there.
If it helps you any better, think of a, b, c, d instead of 1,2,3,4 logic works the same.
You can read some more about input stream and buffer if you are confused because I dont think I can provide a more clear example than this.
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.