您的位置:首页 > 编程语言 > C语言/C++

C++中输入字符串总结

2013-09-17 14:44 211 查看
c/c++中常用到字符串,输入字符串可以使用:getline,指针,数组等方式。一个简单的例子如下:

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
char a[10];
cout<<"please input a string :"<<endl;
cin.getline(a, 10);//输入的字符串可以带空格
cout<<a<<endl;
char b[10];
cout<<"please input a string :"<<endl;
scanf("%s", b);
cout<<b<<endl;
char *p=(char * )malloc(sizeof(10));
memset(p, 0, 10);//指针对应空间清零
cout<<"please input a string :"<<endl;
scanf("%s", p);
cout<<p<<endl;
free(p);
p=NULL;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: