Monday, April 22, 2024

cin statement in c++ programming language

 


The cin object in C++ is an object of class iostream. It is utilized to acknowledge the contribution from the standard information gadget for example console. It is related with the standard C information stream stdin. The extraction operator(>>) is utilized alongside the item cin for understanding sources of info. The extraction administrator separates the information from the item cin which is placed utilizing the console.



Program 1:

Below is the C++ program to implement cin object:

// C++ program to demonstrate the
// cin object
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    string s;
  
    // Take input using cin
    cin >> s;
  
    // Print output
    cout << s;
  
    return 0;
}

Input: 

s=10

 

Output:

s=10

The cin can likewise be utilized with some part works which are as per the following:


cin.getline(char *buffer, int N):

It peruses a flood of characters of length N into the string cradle, It stops when it has perused (N - 1) characters or it tracks down the finish of the document or newline character(\n). The following is the C++ program to execute cin.getline():

// C++ program to illustrate the use
// of cin.getline
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    char name[5];
  
    // Reads stream of 3
    // characters
    cin.getline(name, 3);
  
    // Print output
    cout << name << endl;
  
    return 0;
}


Program 2:

Multiple inputs using the extraction operators(>>) with cin. Below is the C++ program to take multiple user inputs:
 

// C++ program to illustrate the take
// multiple input
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    string name;
    int age;
  
    // Take multiple input using cin
    cin >> name >> age;
  
    // Print output
    cout << "Name : " << name << endl;
    cout << "Age : " << age << endl;
  
    return 0;
}

No comments:

Post a Comment

last post

C language Vs C++ language in 2024

Popular Posts