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

68,827

Solution 1

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.

Solution 2

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.

Solution 3

For a char array use cin.get() because it counts whitespace whereas cin does not. More importantly, cin.get() sets the maximum number of characters to read. For example:

 char foo[25]; //set maximum number of characters 
 cout << "Please type in characters for foo" << endl; 
 cin.get(foo,25);  
 cout << ' ' << foo;  

In this case, you can only type in 24 characters and a null terminator character \0.
I hope this answers your first question. I would personally use a string.

Share:
68,827
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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.