Curriculum
Course: Complete C++ Programming Course
Login

Curriculum

Complete C++ Programming Course

Video lesson

Reading a String From User in C++

In this lesson, you will learn

  • Reading a String From User

  • Examples

 

Reading a String Data

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++.

ReadingString

 

1. Using ‘cin’

The std::string class is part of the C++ Standard Library and provides a convenient way to work with strings.

Example: Reading a Single Word

#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;
}



Explanation:

  • std::cin is the standard input stream.
  • The >> operator reads input until it encounters whitespace (space, tab, or newline).
  • This means it will only read a single word.

 

Example: Reading a Line of Text using a getline() function

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;
}




Explanation:

  • std::getline(std::cin, line) reads the entire line from the input until a newline character is encountered.
  • This is useful for reading full sentences or lines with spaces.

 

2. Using std::cin with char Arrays

This 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.

Example: Reading a Single Word

#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;
}



Example: Reading a Line using a char array

#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;
}



Explanation:

  • std::cin.getline(line, size) reads up to size-1 characters (leaving space for the null terminator) or until a newline character is encountered.
  • Useful for handling C-style strings (character arrays).

 

 

3. Using std::getline with std::cin for Delimited Input

You can use std::getline with a custom delimiter to read input until a specific character is encountered.

Example: Reading Until a Comma

#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;
}



Explanation:

  • std::getline(std::cin, input, delimiter) reads the input until it encounters the specified delimiter (in this case, a comma).
  • This method can be used to read strings separated by any character.

 

Exercises

  1. Ram’s basic salary is input through the keyboard. His dearness allowance is 40% of his basic salary, and his house rent allowance is 20% of his basic salary. Write a program in C++ to calculate his gross salary.

    Hint – Gross Salary = Basic Salary + Dearness Allowance + House Rent Allowance.

  2. 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.

  3. 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.

  4. 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.

  5. The temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.

  6. 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.

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review