In this lesson, you will learn
Reading a String From User
Reading string data in C++ can be done using several methods, each suited to different needs. Here’s an overview of the most common methods for reading strings in C++.

The std::string class is part of the C++ Standard Library and provides a convenient way to work with strings.
#include<iostream>
#include<string>
int main() {
std::string word;
std::cout << "Enter a word: ";
std::cin >> word; // Reads a single word until whitespace
std::cout << "You entered: " << word << std::endl;
return 0;
}
std::cin is the standard input stream.>> operator reads input until it encounters whitespace (space, tab, or newline).This method reads an entire line, including spaces.
#include<iostream>
#include<string>
int main() {
std::string line;
std::cout << "Enter a line: ";
std::getline(std::cin, line); // Reads the entire line including spaces
std::cout << "You entered: " << line << std::endl;
return 0;
}
std::getline(std::cin, line) reads the entire line from the input until a newline character is encountered.
std::cin with char ArraysThis method is based on C-style strings (character arrays) and is less safe compared to std::string. Similar to std::getline, but used with character arrays.
#include<iostream>
int main() {
char word[100];
std::cout << "Enter a word: ";
std::cin >> word; // Reads a single word
std::cout << "You entered: " << word << std::endl;
return 0;
}
#include<iostream>
int main() {
char line[256];
std::cout << "Enter a line: ";
std::cin.ignore(); // Ignore any leftover newline characters in the input buffer
std::cin.getline(line, sizeof(line)); // Reads a line of text
std::cout << "You entered: " << line << std::endl;
return 0;
}
std::cin.getline(line, size) reads up to size-1 characters (leaving space for the null terminator) or until a newline character is encountered.
std::getline with std::cin for Delimited InputYou can use std::getline with a custom delimiter to read input until a specific character is encountered.
#include<iostream>
#include<string>
int main() {
std::string data;
std::cout << "Enter data separated by commas: ";
std::getline(std::cin, data, ','); // Reads input until a comma
std::cout << "You entered: " << data << std::endl;
return 0;
}
std::getline(std::cin, input, delimiter) reads the input until it encounters the specified delimiter (in this case, a comma).
Hint – Gross Salary = Basic Salary + Dearness Allowance + House Rent Allowance.
The distance between two cities (in km.) is input through the keyboard. Write a program in C++ to convert and print this distance in meters, feet, inches, and centimeters.
If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digits of this number.
The length & breadth of a rectangle and the radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle.
The temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.
Enter the marks obtained by a student in five different subjects through the keyboard, and find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum mark that can be obtained by a student in each subject is 100.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.