You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>usingnamespacestd;intmain() {
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;
return0;
}
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>usingnamespacestd;intmain() {
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;
return0;
}
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>usingnamespacestd;intmain() {
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;
return0;
}
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>usingnamespacestd;intmain() {
char ch;
cout << "Please enter a character: ";
cin >> ch;
cout << "Thank you!";
return0;
}
Please enter a character: [Enter]
[Enter]
[Space]
[Tab]
a
Thank you!
Use cin.get() :
#include<iostream>usingnamespacestd;intmain() {
char ch;
cout << "Please enter a character: ";
// cin >> ch;
ch = cin.get(); // Or: cin.get(ch);
cout << "You enter a character: " << ch << "\nThank you!";
return0;
}
Please enter a character: [Space]
You enter a character: [Space]
Thank you!
3. cin.ignore()
Mixingcin >> 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>usingnamespacestd;intmain() {
char ch;
int number;
cout << "Enter a number: ";
cin >> number;
cout << "Enter a character: ";
ch = cin.get();
cout << "Thank you!";
return0;
}
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>usingnamespacestd;intmain() {
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!";
return0;
}
Enter a number: 100
Enter a character: a
Thank you!
The text was updated successfully, but these errors were encountered:
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
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
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).
To work around this problem, you can use a C++ function named
getline
.cin.getline()
:2. cin.get()
To read a single character:
Use
cin
:Problem: will skip over blanks, tabs,
Use
cin.get()
:Will read the next character entered, even whitespace
Use
cin
:Use
cin.get()
:3. cin.ignore()
Mixing
cin >>
andcin.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 thecin
object to skip one or more characters in the keyboard buffer.cin >>
andcin.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.The text was updated successfully, but these errors were encountered: