diff between cin.get() and cin.getline()
Diff between cin.get() and cin.getline()
cin . get() takes the input character by character but getline() is used to get a line from a file line by line
char ch;
cin.get(ch);
while(ch!=’$’){
cout<<ch;
ch=cin.get();
}
for this code
if input file contain
Abcd
efgh$
output is
Abcd
efgh
and for
char str[100];
cin.getline(str,100);
cout<<str;
output is
Abcd
we can also apply delimiter to this (by default it is ‘\n’)
for ex
char str[100];
cin.getline(str,100,’$’);
cout<<str;
output will be
Abcd
efgh
i hope you understand now
in the video we write cin.get();
after taking n
this is because
after n there will be a newline so we have to remove it from input stream that’s why we write cin.get();
otherwise getline read an empty new line
i hope you understand now