char str[20];

cin.getline(str,20)

string str;

getline(cin,str);

据说都可以存储含空格字符串,但我用VC6.0时都出错

以下为粘贴

关于在C++中输入带空格的字符串的方法

yibcs 2012-08-10 20:44:17

此人文章

#include <iostream>

#include <stdio.h>
#include <string>

using namespace std;


void main()
{
char unitName[30];

cout<<"\nPlease enter the unit name:\n";
getchar();
gets(unitName);

cout<<unitName<<endl;

}

用getchar(); 和gets(unitName);的组合,

getchar()是程序等着用户按键,用户输入的字符被存放在键盘缓冲区中,直到用户按回车为止(回车字符也放在缓冲区中)。

而gets()输入是不会遇到空格就停止的函数。


gets()只适用于字符数组,string型变量不能用这个,因为gets()是C语言的函数,不是C++的