在c++里怎么输入空格?

我写了一个char name[31],我需要输入:ni hao
怎么才可以把空格也写进去呢?string的话也是一样操作的吧?

gets()可以,但是长度超过数组长度会缓冲区溢出。
建议用fgets(str,31,stdin);
stdin代表标准输入,31表示最大接受长度,超过这个长度不会溢出,只读取最大长度。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-06-14

用 cin.getline(字符串变量,变量大小)

功能:从键盘输入一个以ENTER键标志结束的字符串.

这样,你就可以输入空格.最后按ENTER提交你输入的字符串。

实现代码:

#include<iostream>

#include<string>

using namespace std;

int main()

{

string ss;

getline(cin,ss);

cout<<ss<<endl;

char ch[25];

cin.getline( ch,25 );

cout<<ch<<endl;

}

本回答被网友采纳
第2个回答  2020-06-14
加个头文件#include<stdio.h>
用C语言中的gets()函数
char str[111];

gets(str);//结束符为回车符('\n')

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网