Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Qingquan-Li opened this issue Jun 8, 2022 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Jun 8, 2022

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 :

    char ch;
    cout << "Strike any key to continue";
    cin >> ch;

    Problem: will skip over blanks, tabs,

  • Use cin.get() :

    cin.get(ch);

    Will read the next character entered, even whitespace


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!
@Qingquan-Li Qingquan-Li added the C++ label Jun 8, 2022
@Qingquan-Li Qingquan-Li changed the title C++: Working with Characters Objects: get line, cin.get, cin.ignore C++: Working with Characters and string Objects: get line, cin.get, cin.ignore Jun 8, 2022
@Qingquan-Li Qingquan-Li changed the title C++: Working with Characters and string Objects: get line, cin.get, cin.ignore C++: Working with Characters and string Objects: getline(), cin.get(), cin.ignore() Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant