Various input functions in C++

This article is used to practice various inputs in C++

topic:

Usage scenarios for different input functions

#include <iostream>
#include <string>

using namespace std;

int main(int *argc, int **argv)
{
	string str;
	char a,c[20];

	getline(cin, str);//Similar to cin.getline(c,20); The entry parameter is a string object, read a line of strings containing spaces
	cout << str;

	gets(c);//Similar to getline(cin, str); but the entry parameter is a character array, read a line of strings containing spaces
	cout << c;

	a=getchar();//Only one character can be read
	cout << a;

	return 0;
}
#include <iostream>

using namespace std;

int main(int *argc, int **argv)
{
	char a,b,c[20],d[2][20];

	cin >> a >> b>>c;//When '\0' '\n' '\t' is encountered in the input stream, the reading ends
	cout << a << " " << b << " " << c << endl;

	cin.get(a);//The entry parameter is one character, that is, only one character can be read
	cout << a << endl;
	cin.get(c, 20);//The entry parameter is a character array, which can read a line of strings containing spaces 19+'\0'
	cout << c << endl;

	cin.getline(c, 20);//Similar to cin.get(c,20);
	cout << c << endl;
	cin.getline(c, 20, 'a');//The character is read to the end of 'a'
	cout << c << endl;
	for (int i = 0; i < 2; i++)//Read a two-dimensional string
		cin.getline (d [i], 20);
	cout << d[0] << " " << d[1] << endl;

	return 0;
}


References:

https://blog.csdn.net/zhanghaotian2011/article/details/8868577



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325861314&siteId=291194637