string字符串输入

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

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

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

cpp 复制代码
getline(cin,name);
cpp 复制代码
//reading more than one word with getline
#if 1
#include<iostream>
#include<string>//要使用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
相关推荐
不想写代码的星星17 小时前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
樱木Plus3 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_6 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星6 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛7 天前
delete又未完全delete
c++
端平入洛8 天前
auto有时不auto
c++
郑州光合科技余经理9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1239 天前
matlab画图工具
开发语言·matlab
西岸行者9 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习