Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

C++: Working with Characters and string Objects: getline(), cin.get(), cin.ignore() #191

Open Qingquan-Li opened 2 years ago

Qingquan-Li commented 2 years ago

Reference:

Book: Starting Out with C++ from Control Structures to Objects, byTony Gaddis, ninth edition


1. getline()

Using cin with the >> operator to input strings can cause problems:

It passes over and ignores any leading whitespace characters (spaces, tabs, or line breaks).

#include <iostream>
#include <string>

using namespace std;

int main() {
    string name;
    string city;

    cout << "Please enter your name: ";
    cin >> name;
    cout << "Enter the city you live in: ";
    cin >> city;

    cout << "Hello, " << name << endl;
    cout << "You live in " << city << endl;
    return 0;
}
Please enter your name: Jake Li
Enter the city you live in: Hello, Jake
You live in Li

To work around this problem, you can use a C++ function named getline.

// This program demonstrates using the getline function
// to read character data into a string object.

#include <iostream>
#include <string>

using namespace std;

int main() {
    string name;
    string city;

    cout << "Please enter your name: ";
    // cin >> name;
    getline(cin, name);
    cout << "Enter the city you live in: ";
    // cin >> city;
    getline(cin, city);

    cout << "Hello, " << name << endl;
    cout << "You live in " << city << endl;
    return 0;
}
Please enter your name: Jake Li
Enter the city you live in: New York
Hello, Jake Li
You live in New York

cin.getline():

// This program demonstrates using the `cin.getline()` function
// to read character data into a `char` object.

#include <iostream>

using namespace std;

int main() {
    char name[50];
    char city[50];

    cout << "Please enter your name: ";
    cin.getline(name, 50);
    cout << "Enter the city you live in: ";
    cin.getline(city, 50);

    cout << "Hello, " << name << endl;
    cout << "You live in " << city << endl;
    return 0;
}
Please enter your name: John Joe
Enter the city you live in: Los Angeles
Hello, John Joe
You live in Los Angeles


2. cin.get()

To read a single character:


Use cin :

#include <iostream>

using namespace std;

int main() {
    char ch;
    cout << "Please enter a character: ";
    cin >> ch;
    cout << "Thank you!";
    return 0;
}
Please enter a character: [Enter]
[Enter]
 [Space]
    [Tab]
a
Thank you!

Use cin.get() :

#include <iostream>

using namespace std;

int main() {
    char ch;
    cout << "Please enter a character: ";
//    cin >> ch;
    ch = cin.get(); // Or: cin.get(ch);
    cout << "You enter a character: " << ch << "\nThank you!";
    return 0;
}
Please enter a character:  [Space]
You enter a character:  [Space]
Thank you!


3. cin.ignore()

Mixing cin >> and cin.get() in the same program can cause input errors that are hard to detect.

To skip over unneeded characters that are still in the keyboard buffer, use cin.ignore() .

The cin.ignore() function tells the cin object to skip one or more characters in the keyboard buffer.

cin.ignore();         // skip next char

// cin.ignore(n, c);  skip the next n characters, or until the character c is encountered.
cin.ignore(10, '\n'); // skip the next 10 char. or until a '\n'
// This program demonstrates a problem that occurs
// when you mix cin >> with cin.get().

#include <iostream>

using namespace std;

int main() {
    char ch;
    int number;
    cout << "Enter a number: ";
    cin >> number;
    cout << "Enter a character: ";
    ch = cin.get();
    cout << "Thank you!";
    return 0;
}
Enter a number: 100[Enter]
Enter a character: Thank you!

cin >> and cin.get use slightly different techniques for reading data.

Pressing the [Enter] key causes a newline character \n to be stored in the keyboard buffer.

The cin >> statement begins reading the data that the user entered, and stops reading when it comes to the newline character. The newline character is not read, bur remains in the keyboard buffer.

1 0 0 \n
// This program successfully uses both
// cin >> and cin.get() for keyboard input.

#include <iostream>

using namespace std;

int main() {
    char ch;
    int number;
    cout << "Enter a number: ";
    cin >> number;

    cin.ignore(); // Skip the newline character

    cout << "Enter a character: ";
    ch = cin.get();
    cout << "Thank you!";
    return 0;
}
Enter a number: 100
Enter a character: a
Thank you!