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:
#include <iostream>
using namespace std;
int main()
{
string s;
cin >> s;
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(): #include <iostream>
using namespace std;
int main()
{
char name[5];
cin.getline(name, 3);
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: #include <iostream>
using namespace std;
int main()
{
string name;
int age;
cin >> name >> age;
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
return 0;
}
|
|
No comments:
Post a Comment