C++ Beginner

cpp_006_Introduction to getline and cin.getline in C++: Handling User Input with Spaces

codeaddict 2025. 1. 5. 23:12

In C++, reading strings can be tricky when the input contains spaces. For instance, the default cin input method stops reading input when it encounters a whitespace character. In this tutorial, we’ll explore how to handle input with spaces using getline() and cin.getline().

1. Problem with cin and Spaces

By default, when you use cin >> variable, it reads input until it encounters a whitespace (space, tab, or newline). This makes it difficult to read full sentences or strings with spaces.

Code Example:

Example 1: Input with Multiple Words

Let’s examine an example where multiple words are entered, and we see how cin >> processes them:

#include <iostream>
using namespace std;

int main() {
    string firstWord;
    string leftover1;
    string leftover2;
    string leftover3;
    string leftover4;

    // Using cin to read input (stops at space)
    cout << "Enter a greeting: ";
    cin >> firstWord;  // Reads the first word only
    cout << "You entered: " << firstWord << endl;

    // Reading leftover input after the first word
    cin >> leftover1;  // Reads the next word
    cout << "Leftover input: " << leftover1 << endl;

    cin >> leftover2;  // Reads the next word
    cout << "Leftover input: " << leftover2 << endl;

    cin >> leftover3;  // Reads the next word
    cout << "Leftover input: " << leftover3 << endl;

    cin >> leftover4;  // Reads the next word
    cout << "Leftover input: " << leftover4 << endl;

    return 0;
}

Example 1: Case 1 — Input: “fd dfd”

If you input fd dfd and press Enter:

  • The first cin >> firstWord; reads fd (only the first word before the space).
  • The following cin >> leftoverX; calls will read the next words from the leftover input.

Output:

Enter a greeting: hi there
You entered: hi
Leftover input: there

Explanation:

  • cin >> firstWord; captures hi (first word).
  • The space after hi is left in the input buffer.
  • cin >> leftover1; reads there, as it’s the next available word in the buffer.
  • Since there are no more words in the buffer after there, the remaining cin >> leftover2, cin >> leftover3, and cin >> leftover4 calls result in no input being captured.

Example 1: Case 2 — Input: “hello today is cold, what are you wearing”

Now, if you input a longer sentence like hello today is cold, what are you wearing:

  • cin >> firstWord; reads hello (the first word before the first space).
  • The following cin >> leftoverX; calls will read one word at a time from the remaining input.

Output:

Enter a greeting: hello today is cold, what are you wearing
You entered: hello
Leftover input: today
Leftover input: is
Leftover input: cold,
Leftover input: what

Explanation:

  • cin >> firstWord; captures hello.
  • The space after hello is left in the buffer, so the next cin >> leftover1; reads today.
  • After reading today, the space after it leaves the buffer, so cin >> leftover2; captures is.
  • This continues until all words are processed, one by one, in each call to cin >> leftoverX;.

This shows that cin >> captures one word at a time, and any remaining words are handled in subsequent calls to cin >>.


2. When to Use getline() to Capture Full Sentences

To handle input with spaces properly and capture full sentences or phrases, cin >> is not sufficient because it only reads one word at a time. Instead, we use getline() to read the entire line of input, including spaces.

Modified Code Using getline() to Capture Full Sentences:

#include <iostream>
using namespace std;

int main() {
    string fullInput;

    // Using getline() to read a full line of input, including spaces
    cout << "Enter a full sentence: ";
    getline(cin, fullInput);  // Reads the entire line
    cout << "You entered: " << fullInput << endl;

    return 0;
}

Output:

Enter a full sentence: hello today is cold, what are you wearing
You entered: hello today is cold, what are you wearing

Explanation:

  • getline(cin, fullInput); reads the entire line of input, including spaces between words.
  • It captures the full sentence hello today is cold, what are you wearing in the fullInput string.

3. Using cin.getline() for Fixed-Length Input

cin.getline() is another version of getline() but with an additional feature. It allows you to specify the maximum number of characters that can be read. If the input exceeds this limit, it will be truncated, and any excess characters will remain in the input buffer for future use.

Code Example:

#include <iostream>
using namespace std;

int main() {
    char greeting[50];  // Buffer size

    // Using cin.getline() to read up to 50 characters
    cout << "Enter a greeting: ";
    cin.getline(greeting, 50);
    cout << "You entered: " << greeting << endl;

    return 0;
}

Output:

Enter a greeting: Hello there, how are you doing?
You entered: Hello there, how are you doing?

4. Key Differences Between getline() and cin.getline()

  • getline(cin, string) works with std::string and automatically adjusts the string size.
  • cin.getline(char array[], size) works with character arrays and requires you to define a fixed size for the buffer.

Conclusion

Handling user input in C++ can be challenging when the input contains spaces. The default cin >> behavior stops at spaces, but by using getline() or cin.getline(), you can easily capture full sentences or control the size of the input buffer. Understanding these functions helps you better manage user input and avoid common pitfalls.