GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — cin Statement and its Use in C++ Program | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
C++ Programming Tutorial
  • C++ Basic Concepts
    • Introduction
    • Variable
    • Data Types and Modifiers
    • Naming Conventions
    • Integer Variable
    • Float and Double Variables
    • Character Variable
    • Boolean Variable
    • Constant Variable
    • Typecasting
  • Operators in C++
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Operator Precedence
  • Input and Output in C++
    • Environment Setup
    • Program Basic Structure
    • cout Statement
    • cin Statement
    • Comments
  • Decision Making in C++
    • Decision Making
    • if Statement
    • if else Statement
    • if else if Statement
    • Conditional Operator
    • switch case Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in C++
    • What are Loops
    • for Loop
    • while Loop
    • do while Loop
  • Arrays in C++
    • One Dimensional Array
    • Two Dimensional Array
  • String in C++
    • C-Style String
    • C-Style String Methods
    • String
    • String Methods
  • Searching in C++
    • Linear Search
    • Binary Search
  • Sorting in C++
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Pointer in C++
    • Pointer
    • Dynamic Memory Allocation
    • References
  • User Defined Datatype in C++
    • Structure
    • Union
    • Enum
  • Function in C++
    • Function
    • Recursive Function
    • Function Overloading
    • Storage Classes
    • User-Defined Header Files
  • Data Structures in C++
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • OOPs in C++
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Access Specifiers
    • Constructor
    • Destructor
    • Static Variables
    • Static Methods
    • Friend Function
    • Friend Class
    • Inheritance
    • Types of Inheritance
    • Polymorphism
    • Abstract Class
  • File Handling in C++
    • Text File Handling
    • Binary File Handling

bookmark
Table of Contents

    cin Statement and its Use in C++ Program

    In this lesson, we will look at what is cin statement in C++ programming and how it works along with some example.

    What is cin Statement

    The cin statement in C++ is used for input operations. It is part of the standard input/output stream library, which allows program to read input from the standard input device, typically the keyboard. The cin is defined in the <iostream> header file.

    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    Let's see an example of how to use cin to read different types of data from the keyboard.

    Example
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int age;
        float height;
        double weight;
        char initial;
    
        // Ask the user to enter their age
        cout<<"Enter your age: ";
        cin>>age;
    
        // Ask the user to enter their height
        cout<<"Enter your height in meters: ";
        cin>>height;
    
        // Ask the user to enter their weight
        cout<<"Enter your weight in kilogram: ";
        cin>>weight;
    
        // Ask the user to enter the first character of their name
        cout<<"Enter the first character of your name: ";
        cin>>initial;
    
        // Print the values entered by the user
        cout<<"\nYou entered:\n";
        cout<<"Age: "<<age<<endl;
        cout<<"Height: " <<height<<" meters"<<endl;
        cout<<"Weight: "<<weight<<" kilogram"<<endl;
        cout<<"Initial: "<<initial<<endl;
    
        return 0;
    }
    Output
    Enter your age: 15
    Enter your height in meters: 3.423
    Enter your weight in kilogram: 45.21543
    Enter the first character of your name: R
    
    You entered:
    Age: 15
    Height: 3.42 meters
    Weight: 45.22 kilogram
    Initial: R
    What Happens in the Example
    1. Prompting the User:
    cout is used to ask the user to enter their age, height, weight, and initial.
    2. Reading Input:
    cin>>age; reads a number input by the user and stores it in the variable age.
    cin>>height; reads a number input by the user and stores it in the variable height.
    cin>>weight; reads a number input by the user and stores it in the variable weight.
    cin>>initial; reads a single character and stores it in the variable initial.
    3. Displaying the Input:
    cout shows the values entered by the user, confirming they were read correctly.
    Note

    The >> is called the Extraction Operator, which is used with the cin statement to extract or read data from an input stream, such as the keyboard.

    Handling Multiple Inputs in a Single cin Statement

    We can read multiple values in a single cin call by using multiple extraction operator >>. Here is an example given below:

    Example
    #include <iostream>
    #include <iomanip>    // For setting precision
    using namespace std;
    
    int main()
    {
        int num1, num2, num3;
        float num4;
        double num5;
    
        // Ask the user to enter three integer numbers, one float and one double type number
        cout<<"Enter three integer numbers, one float and one double type number:"<<endl;
        cin>>num1>>num2>>num3>>num4>>num5;
    
        // Print the values entered by the user
        cout<<"\nYou entered:\n";
        cout<<"1st number: "<<num1<<endl;
        cout<<"2nd number: "<<num2<<endl;
        cout<<"3rd number: "<<num3<<endl;
    
        cout<<fixed<<setprecision(2);      // Display the number with 2 digits after the decimal point
        cout<<"4th number: "<<num4<<endl;
    
        cout<<fixed<<setprecision(8);      // Display the number with 8 digits after the decimal point
        cout<<"5th number: "<<num5<<endl;
    
        return 0;
    }
    Output
    Enter three integer numbers, one float and one double type number:
    45
    95
    78
    12.33
    8.62143624
    
    You entered:
    1st number: 45
    2nd number: 95
    3rd number: 78
    4th number: 12.33
    5th number: 8.62143624

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.