Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between cin and cin.get() for char array

I have these 2 codes:

char a[256];
cin>>a;
cout<<a;

and

char a[256];
cin.get(a,256);cin.get();
cout<<a;

and maybe, relative to the second one without cin.get();

char a[256];
cin.get(a,256);
cout<<a;

My question is (first one) : for a char array, what should i use? cin or cin.get()? And why should i use cin.get(); with no parameter after my char initialisation?

And my second question is: my c++ teacher taught me to use every time cin.get() for initialisation chars and AFTER every initialisation char array or int array or just int or whatever, to again put cin.get(); after it. That's what i wanted to ask initially.

So, now i got these 2: In this case, without cin.get() after the integer initialisation, my program will break and i can't do anymore my char initialisation.

int n;
cin>>n;
char a[256];
cin.get(a,256); cin.get();  // with or without cin.get();?
cout<<a;

And the correct one:

int n;
cin>>n; cin.get();
char a[256];
cin.get(a,256); cin.get(); // again, with or without?
cout<<a;

So, what's the matter? Please someone explain for every case ! Thank you.

like image 931
Ruben P Avatar asked Apr 14 '15 14:04

Ruben P


People also ask

What is the difference between CIN and CIN get ()?

Generally, cin with an extraction operator (>>) terminates when whitespace is found. However, cin. get() reads a string with the whitespace.

Does CIN work with char?

Cin With Member Functionsget(char &ch): Reads a character from the input and stores it in ch. cin. getline(char *buffer, int length): Reads a stream of characters into the string buffer, stopping when it reaches length-1 characters, an end-of-line character ('n'), or the file's end.

What is get () in C++?

gets() prototype char* gets(char* str); The gets() function reads characters from stdin and stores them in str until a newline character or end of file is found.

Does CIN get () read newline?

getline(cin, newString); begins immediately reading and collecting characters into newString and continues until a newline character is encountered. The newline character is read but not stored in newString.


2 Answers

They do different things, so choose whichever does what you want, or the better alternatives given below.

The first cin>>a; reads a single word, skipping over any leading space characters, and stopping when it encounters a space character (which includes the end of the line).

The second cin.get(a,256);cin.get(); reads a whole line, then consumes the end-of-line character so that repeating this will read the next line. cin.getline(a,256) is a slightly neater way to do this.

The third cin.get(a,256) reads a whole line but leaves the end-of-line character in the stream, so that repeating this will give no more input.

In each case, you'll get some kind of ill behaviour if the input word/line is longer than the fixed-size buffer. For that reason, you should usually use a friendlier string type:

std::string a;
std::cin >> a;              // single word
std::getline(std::cin, a);  // whole line

The string will grow to accommodate any amount of input.

like image 54
Mike Seymour Avatar answered Sep 30 '22 04:09

Mike Seymour


The problem, most likely, is in the way you enter the values later on. The cin.get() after every initialization is there to "grab" the newline character that gets put in the stream every time you press enter. Say you start entering your values like this:

2 

a b c d...

Assuming you have pressed enter after 2, the newline character was also put on the stream. When you call cin.get() after that, it will grab and discard the newline character, allowing the rest of your code to properly get the input.

To answer your first question, for an array, you should use cin.get instead of the overloaded operator >> cin>> as that would only grab a single word, and it would not limit the amount of characters grabbed, which could lead to an overflow and data corruptions / program crashing.

On the other hand, cin.get() allows you to specify the maximum number of characters read, preventing such bugs.

like image 24
Nazara Avatar answered Sep 30 '22 03:09

Nazara