天天看点

C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

C++输入字符串的几种方法

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

C++中的字符串输入方式有:

1.cin >> 
2.cin.get()
3.cin.getline()
4.getline()
5.gets()
6.getchar()
           

cin

第一种用法是逐个输入数字或者字符,中间用空格隔开。这是最基本的。

第二种用法是读入一个字符串,遇到”空格、“TAB”、“回车”都会结束,例如:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s;
	cin >> s;
	cout << s;
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

cin.get()

用法1:cin.get(字符变量名)可以用来接受字符

#include<iostream>
#include<string>
using namespace std;
int main()
{
	char ch;
	cin.get(ch);//或者ch = cin.get(); 
	cout << ch << endl;//只能输出一个字符 
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

用法2:cin.get(字符数组名,接受字符数目)用来接收一行字符串,可以接收空格

#include<iostream>
using namespace std;
int main()
{
	char s[50];
	cin.get(s, 20);//可以输入多个单词,中间空格隔开,空格也计算在内
	cout << s << endl; 
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

用法3:cin.get(无参数),无参数主要用于收纳输入流中的不需要的字符,或者收纳回车,弥补cin.get(字符数字名,接收字符数目)的不足。

cin.getline()

用法1:接收一个字符串,可以接受空格并输出

#include<iostream>
using namespace std;
int main()
{
	char s[25];
	cin.getline(s,15);
	cout << s << endl;	
	
	return 0;
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

用法2:cin.getline(接受字符串到n, 接收个数6,结束字符)

#include<iostream>
using namespace std;
int main()
{
	char s[25];
	cin.getline(s,25,'n');
	cout << s << endl;	
	
	return 0;
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

用法3:cin.getline(s[i], n)

应用于二维数组中
#include<iostream>
using namespace std;
int main()
{
	char s[25][45];
	for(int i = 0; i < 3 ; i++ )
	{
		cout << "请输入第" << i + 1 << "个字符串" << endl;
		cin.getline(s[i],8);
	}
	cout << endl;
	
	for(int i = 0; i < 3; i ++ )
	cout << "输出s[" << i << "]的值:" << s[i] <<endl;
	
	return 0;
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

getline()

接收一个字符串,可以接受空格并输出,在头文件< string >中

#include<iostream>
using namespace std;
int main()
{
	string s;
	getline(cin, s);
	
	cout << s;
	
	return 0;
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

gets()

用法1:

接受一个字符串,可以接受空格并输出
#include<iostream>
using namespace std;
int main()
{
	char s[50];
	gets(s);//输入什么,输出什么 
	cout << s << endl; 
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

用法2:类似于上面getline()中的多维数组

#include<iostream>
using namespace std;
int main()
{
	char s[25][45];
	for(int i = 0; i < 3 ; i++ )
	{
		cout << "请输入第" << i + 1 << "个字符串" << endl;
		gets(s[i]);
	}
	cout << endl;
	
	for(int i = 0; i < 3; i ++ )
	cout << "输出s[" << i << "]的值:" << s[i] <<endl;
	
	return 0;
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

getchar()

接收一个字符
#include<iostream>
using namespace std;
int main()
{
	char ch;
	ch = getchar();//不能写作getchar(ch)
	cout << ch << endl; 
	
	return 0;
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

循环输入时,作为判断条件

#include<iostream>
using namespace std;
int main()
{
	char ch;
	while((ch = getchar()) != '\n')
	{
		cout << ch;
	}
	
	return 0;
}
           
C++中输入字符串的几种方法cincin.get()cin.getline()getline()gets()getchar()

在字符串这比较晕的,可以了解了解~~~

继续阅读