string字符串输入

用cin输入string字符串时,默认遇到空格回车制表符等空白字符即字符串输入结束。

因此遇到带空格字符的字符串就会出现问题,此时需要用到getline函数,getline()是遇回车符输入结束。

string字符串的基本输入格式是:

getline(cin,name);
//reading more than one word with getline
#if 1
#include
#include//要使用string类,必须在程序中包含头文件string。
using namespace std;

int main()
{
	//const int Arsize = 20;
	string name;
	string dessert;

	cout << "Enter your name:\n";
	getline(cin,name);
	cout << "Enter your favorite dessert:\n";
	getline(cin,dessert);
	cout << "I have some delicious " << dessert << " for you, " << name << "." << endl;
	system("pause");
	return 0;
}
#endif

你可能感兴趣的:(c++,学习,开发语言,计算机网络,软件工程,visualstudio)