C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In this tutorial, we will learn how to use cin.getline()
to read a line of strings from the user in C++. The cin.getline()
function is a useful method for reading input lines that include whitespace characters, such as spaces or tabs, which are typically ignored by the >>
operator.
Let's go through a simple example to demonstrate how to use cin.getline()
:
First, include the necessary headers for the program:
#include <iostream> #include <cstring>
cin.getline()
:Next, create a character array (or a buffer) to store the input line and use cin.getline()
to read the line of strings from the user:
int main() { const int bufferSize = 256; char inputLine[bufferSize]; std::cout << "Please enter a line of text: "; std::cin.getline(inputLine, bufferSize); std::cout << "You entered: " << inputLine << std::endl; return 0; }
In this example, we create a buffer inputLine
with a size of 256 characters to store the input line. We then use cin.getline()
to read a line of strings from the user and store it in inputLine
. The bufferSize
parameter indicates the maximum number of characters (including the null terminator) that can be read into the buffer. After reading the input line, we output the entered text.
Note that if the user input exceeds the buffer size, cin.getline()
will only read the first bufferSize - 1
characters and leave the rest in the input stream.
That's it for our tutorial on using cin.getline()
in C++ to read a line of strings. The cin.getline()
function is a helpful tool for reading input lines with whitespace characters, providing a more flexible way to handle user input in your programs.
How to Use cin.getline()
for Reading a Line of Strings in C++:
cin.getline()
is used to read a line of input from the standard input (keyboard) and store it as a string.Example:
#include <iostream> #include <cstring> int main() { const int bufferSize = 100; char input[bufferSize]; std::cout << "Enter a line of text: "; std::cin.getline(input, bufferSize); std::cout << "You entered: " << input << std::endl; return 0; }
Handling Spaces and Whitespace with cin.getline()
in C++:
cin.getline()
reads the entire line, including spaces and whitespace characters.Example:
#include <iostream> #include <cstring> int main() { const int bufferSize = 100; char input[bufferSize]; std::cout << "Enter a sentence: "; std::cin.getline(input, bufferSize); std::cout << "You entered: " << input << std::endl; return 0; }
Reading Multiple Lines with cin.getline()
in C++:
cin.getline()
in a loop to read multiple lines.Example:
#include <iostream> #include <cstring> int main() { const int bufferSize = 100; char input[bufferSize]; for (int i = 0; i < 3; ++i) { std::cout << "Enter line " << i + 1 << ": "; std::cin.getline(input, bufferSize); std::cout << "You entered: " << input << std::endl; } return 0; }
Error Handling with cin.getline()
in C++:
cin.getline()
returns a reference to the input stream, which can be used for error checking.Example:
#include <iostream> #include <cstring> int main() { const int bufferSize = 100; char input[bufferSize]; std::cout << "Enter a line of text: "; if (!std::cin.getline(input, bufferSize)) { std::cerr << "Error reading input." << std::endl; return 1; } std::cout << "You entered: " << input << std::endl; return 0; }
Using Delimiter with cin.getline()
for Tokenizing Input in C++:
Example:
#include <iostream> #include <cstring> int main() { const int bufferSize = 100; char input[bufferSize]; std::cout << "Enter a list of items (comma-separated): "; std::cin.getline(input, bufferSize, ','); std::cout << "You entered: " << input << std::endl; return 0; }
Reading and Parsing a Line of Input with cin.getline()
in C++:
std::stringstream
) to parse the input.Example:
#include <iostream> #include <sstream> #include <string> int main() { const int bufferSize = 100; char input[bufferSize]; std::cout << "Enter two numbers (space-separated): "; std::cin.getline(input, bufferSize); std::stringstream ss(input); int num1, num2; ss >> num1 >> num2; std::cout << "Sum: " << num1 + num2 << std::endl; return 0; }
Skipping Newline Characters with cin.getline()
in C++:
cin.ignore()
to skip newline characters left in the input buffer.Example:
#include <iostream> #include <cstring> int main() { const int bufferSize = 100; char input[bufferSize]; std::cout << "Enter a line of text: "; std::cin.getline(input, bufferSize); // Clear any remaining characters in the input buffer std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "You entered: " << input << std::endl; return 0; }
Example:
#include <iostream> #include <limits> int main() { const int bufferSize = 100; char input[bufferSize]; std::cout << "Enter a positive integer: "; while (true) { if (std::cin.getline(input, bufferSize) && std::cin.gcount() > 0 && std::strspn(input, "0123456789") == std::strlen(input)) { // Valid input break; } else { std::cout << "Invalid input. Please enter a positive integer: "; std::cin.clear(); // Clear error flags std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input } } int num = std::atoi(input); std::cout << "You entered: " << num << std::endl; return 0; }