C ++: Detailed explanation of how to enter a string


Several common methods of inputting strings in C ++ are as follows:

cin、cin.get()、cin.getline()、getline()、gets()、getchar()

cin >>

  1. Enter a number
#include <iostream>
using namespace std;

int main()	
{
	int a, b;
	cin >> a >> b;	//输入:2[回车]3[回车]
	cout << "a + b = " << a + b << endl;//输出:a + b = 5

	return 0;
}
  1. Receive a character string, it will end when it encounters "space", "Tab" and "carriage return"
#include <iostream>
using namespace std;

int main()	
{
	char arr[20];
	cin >> arr;
	cout << "arr: " << arr << endl;
	//输入:jkljkl jkljkl 
	//遇空格结束,所以不能输入多个单词
	//输出:jkljkl
	return 0;
}

cin.get()

  1. cin.get (character variable name) can be used to receive a character
#include <iostream>
using namespace std;

int main()	
{
	char ch;
	ch = cin.get();
	//cin.get(ch);
	cout << "ch = " << ch << endl;

	return 0;
}

//输入:jljkljkl
//输出:j
  1. cin.get (character array name, number of received characters) can be used to receive a line of strings, can receive spaces, and automatically receives a '\ 0'
#include <iostream>
using namespace std;

int main()	
{
	char str[20] = { 0 };
	cin.get(str, 20);	//类似于 getline
	cout << "str: " << str << endl;

	return 0;
}
//输入:jkl jkl jkl
//输出:jkl jkl jkl

//输入:abcdeabcdeabcdeabcdeabcde (输入25个字符)
//输出:abcdeabcdeabcdeabcd (接收19个字符 + 1个'\0')
  1. cin.get (no parameter) is used to discard the unwanted characters in the input stream, or discard the carriage return to make up for the shortcomings of cin.get (character array name, number of received characters)
#include <iostream>
using namespace std;

int main()
{
	char str[10] = { 0 };
	cin.get(str, 10);
	cin.get();	//用于吃掉回车,相当于 getchar()
	cout << "str: " << str << endl;

	cin.get(str, 5);
	cout << "str: " << str << endl;

	return 0;
}

//assbdakss
//str: assbdakss
//sdadadsa
//str: sdad

int main()	
{
	char str[20] = { 0 };
	cin.get(str, 20);
	//cin.get();	//用于吃掉回车,相当于 getchar()
	cout << "str: " << str << endl;

	cin.get(str, 5);
	cout << "str: " << str << endl;

	return 0;
}

//hasdjaksasdhaksd
//str: hasdjaksa
//str: sdha

cin.getline ()

  1. Receive a string, you can receive spaces and output, automatically receive a '\ 0'
#include <iostream>
using namespace std;

int main()	
{
	char arr[20] = { 0 };
	cin.getline(arr, 5);
	cout << "arr: "  << arr << endl;

	return 0;
}

//输入:jkljkljkl
//输出:jklj
//接受5个字符到m中,其中最后一个为'\0',所以只看到4个字符输出;

  1. cin.getline () actually has three parameters, cin.getline (receive string, receive number, end character)
    When the third parameter defaults, the system default is '\ n'
#include <iostream>
using namespace std;

int main()	
{
	char arr[10] = { 0 };
	cin.getline(arr, 5, 's');
	cout << "arr: " << arr << endl;

	return 0;
}

//当输入jlkjkljkl时输出jklj,输入jkaljkljkl时,输出jk
  1. When used in multi-dimensional arrays, you can also use cin.getline (arr [i], 10)
#include <iostream>
using namespace std;

int main()	
{
	char arr[3][20] = { 0 };
	for (int i = 0; i < 3; i++)
	{
		cout << "请输入第"<< i + 1 <<"个字符串: " << endl;
		cin.getline(arr[i], 20);
	}
	cout << endl;
	for (int i = 0; i < 3; i++)
	{
		cout << "输出 arr[" << i << "] 的值: " << arr[i] << endl;
	}

	return 0;
}

请输入第1个字符串:
kskr1
请输入第2个字符串:
kskr2
请输入第3个字符串:
kskr3
​
输出m[0]的值:kskr1
输出m[1]的值:kskr2
输出m[2]的值:kskr3

getline()

Receive a string, you can receive spaces and output, including header files #include <string>

#include <iostream>
using namespace std;

int main()	
{
	string str;
	getline(cin, str);
	cout << "str = " << str << endl;

	return 0;
}

与 cin.getline() 类似,但是 cin.getline() 属于 iostream 流,而 getline() 属于 string 流,是不一样的两个函数

gets()

gets () receives a string, can receive spaces and output, need to include #include <string>

#include <iostream>
#include <string>
using namespace std;

int main()	
{
	char arr[20] = { 0 };
 	gets(arr);		//不能写成 arr = gets()
	cout << arr << endl;

	return 0;
}

However, the above code can not compile in VS, I added #include <cstdio>or #include <stdio.h>after the first still does not recognize the file gets () function, and later changed .c file test code is as follows:

#include <stdio.h>

int main()	
{
	char arr[20] = { 0 };
	gets(arr);		//不能写成 arr = gets()
	printf("arr = %s\n", arr);

	return 0;
}

getchar()

Receive a character, including #include <string>

#include <iostream>
#include <string>
using namespace std;

int main()	
{
	char ch;
	ch = getchar();	//不能写成 getchar(ch)
	cout << "ch = " << ch << endl;

	return 0;
}

输入:jkljkljkl
输出:j
​
//getchar()是C语言的函数,C++也可以兼容,但是尽量不用或少用;
Published 152 original articles · praised 45 · 10,000+ views

Guess you like

Origin blog.csdn.net/AngelDg/article/details/105042844