what is cin.get()? without parameters? and what is use?
how is it different from cin?
Charactr array doubt
Hey @Shreya-Gupta-2383169445069382
cin.get() is different from cin() because cin.get() reads a string with the whitespace.
Syntax:
cin.get(string_name, size);
eg:
#include
using namespace std;
int main()
{
char name[25];
cin.get(name, 25);
cout << name;
return 0;
}
Input:
Geeks for Geeks
Output:
Geeks for Geeks
while in case of cin:
#include
using namespace std;
int main()
{
char name[25];
cin >> name;
cout << name;
return 0;
}
Input:
Geeks for Geeks
Output:
Geeks
Whats the use of cin. Get without parameters
@Shreya-Gupta-2383169445069382
The cin.get() member function with no arguments returns the next character from the input. That is you use it in this way:
ch = cin.get();
For example:
#include
using namespace std;
int main()
{
char ch;
while ( (ch = cin.get()) != EOF)
{
cout << "ch: " << ch << endl;
}
cout << “\nDone!\n”;
return 0;
}
This program reads a file character by character until EOF(end of file) is encountered.
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.
“helloWorld”
if you want to take input character by character using cin, you won’t be able to do it
cin would keep reading until it encounters a space, so essentially cin reads word-by-word.
However, you could use cin.get() to read character-by-character
cin.get() by default (no parameters) returns one character but you can parse paramters into it as you wish
you could do c = cin.get(); it’ll save the next char into variable c, the char could even be a whitespace
cin.get() method doesn’t stop at the white space unlike the normal cin
you could do cin.get( c ) to achieve the same thing as above
you can also read a char array of particular size with cin.get() as follows:
char carr[10];
cin.get(carr, 3);
this will read only 2 chars, in general cin.get(carr, n) reads n-1 chars