Curriculum
Course: Complete C++ Programming Course
Login

Curriculum

Complete C++ Programming Course

Video lesson

Working With Inputs and Outputs in C++

In this lesson, you will learn

  • Inputs and Output Operations in C++
  • Examples

 

Introduction

In C++, input and output operations are primarily handled using streams, which are objects that allow data to flow into or out of the program.

The most commonly used stream objects are:

  1. std::cout: Standard output stream, typically used for writing output to the screen.
  2. std::cin: Standard input stream, typically used for reading input from the keyboard.

 

1. Output Using cout

  • It is a predefined object of ostream(output stream) class (pronounced “C out”) which is a part of the standard library <iostream>.
  • In C++, std::cout is used to output data to the standard output stream, which is typically the console or terminal.
  • The << operator, known as the insertion or put-to operator, is used with  std::cout to send data to the output stream.

cout output

 

Example: Basic Usage of std::cout

#include<iostream>

int main() {
    int number = 10;
    double pi = 3.14159;
    std::string message = "Hello, World!";
    
    // Outputting different types of data
    std::cout << "Number: " << number << std::endl;
    std::cout << "Value of pi: " << pi << std::endl;
    std::cout << "Message: " << message << std::endl;

    return 0;
}


 

Cascading std::cout

Cascading with std::cout involves using multiple insertion operators (<<) in a single statement to concatenate several outputs. This approach can make the code more readable and concise.

Example of Cascading std::cout

#include<iostream>

int main() {
    int age = 25;
    double height = 175.5;
    std::string name = "Alice";
    
    // Cascading output with std::cout
    std::cout << "Name: " << name 
              << ", Age: " << age 
              << ", Height: " << height 
              << " cm" << std::endl;
    
    return 0;
}



 

2. Input with cin

  • It is a predefined object of the istream class (pronounced “C in”), which is a part of the standard library <iostream>.
  • It receives input from the standard input device, typically the keyboard.
  • The cin object reads data from the input buffer and stores it in variables for further use.

 

Syntax

The basic syntax for using cin involves the extraction operator or get from (>>), which directs the input from the keyboard into the specified variable.

For example:

int age;

std::cin >> age;

It reads an integer value from the user and stores it in the variable age. The cin object can handle various data types, including integers, floats, characters, and strings.

cin input

 

Example: Reading from std::cin

#include<iostream>

int main() {
    int number;
    std::cout << "Enter an integer: ";
    std::cin >> number;
    std::cout << "You entered: " << number << std::endl;
    return 0;
}

In this example:

  • std::cout << "Enter an integer: " Prints a prompt to the screen.
  • std::cin >> number; reads an integer from the user and stores it in the variable number.
  • std::cout << "You entered: " << number << std::endl; Outputs the entered number to the screen.

 

Cascading std::cin

Cascading std::cin with multiple extraction operators (>>) allows you to read various values from the input stream in a single statement.

This can be useful for reading several pieces of data in sequence.

Example: Cascading std::cin For Multiple Inputs

#include<iostream>

int main() {
    int age;
    double height;
    std::string name;
 
    // Reading multiple values in a single statement
    std::cout << "Enter your name, age, and height (separated by spaces): ";
    std::cin >> name >> age >> height; // Reads all three values
 
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Height: " << height << " cm" << std::endl;
 
    return 0;
}

Output:

Enter your name, age, and height (separated by spaces): Amit
28
170
Name: Amit
Age: 28
Height: 170 cm

 


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