《c++primer plus》第6版第四章复合类型课后练习
第三题
题目需求:编写一个程序,它要求用户首先输入其名,再输入其姓。然后程序使用一个逗号和空格组合起来,并存储和显示组合结果。请使用string对象和头文件string 中的函数,下面是该程序运行的情况:
Enter your first name:Flip
Enter your last nae:Fleming
Here's the information in a single string:Fleming,Flip
本题知识点:
字符串输入输出
代码贴上:#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
int main()
{
using namespace std;
string Name1;
string Name2;
cout << "Enter your first name:\n";
getline(cin,Name1);
cout << "Enter your last name:\n";
getline ( cin,Name2);
cout << "Here's the information in a single string:" << Name2<<","<<" "<<Name1;
return 0;
}
运行结果:
r
如果有建议或错误,欢迎指出改正