C++ tutorial 02 - cin

In C++, the cin object belongs to the iostream class. It accepts input from a normal input device. It's linked to stdin, the standard C input stream. For reading inputs, the extraction operator(>>) is combined with the object cin.

Code01 :

//Header files

#include <iostream>

 

//Declare namespace

using namespace std;

 

//main function

int main() {

 

  string item;

  int count;

 

  //enter input using cin

  cin >> item>> count;

 

  //output to the console window

  cout << "Item : " << item <<endl;

  cout << "Count : " << count <<endl;


  //end of the program

  return 0;

}

 

Input

Output



The get() method is used to obtain a character array. It has white space characters in it. When whitespace is discovered, cin with an extraction operator (>>) usually terminates.

cin.get(string_name, size);

 Code02 :

#include <iostream>

using namespace std;

 

int main()

{

    char item[100];

    int count;


    //get function

    cin.get(item, 5);

    cin>>count;

    cout << item <<" count is : " << count <<endl;

    return 0;

}

 

Input


Output



Other cin member functions :

  • cin.getline()
  • cin.read()
  • cin.ignore(int n)
  • cin.eof()

 



Post a Comment

Previous Post Next Post