1.输入与输出
C++中使用cin和cout来从控制台输入变量以及打印内容到控制台上,它们都是属于命名空间std,并且在库iostream中的。
2.实践案例
输入一个名字,打印一串问候内容。
#include<iostream>
#include<string>
using namespace std;
int main() {
cout<<"Please enter your first name:"<<endl;
string name;
cin>>name; //only read a word
const string greeting = "Hello," + name + "!";
const string spaces(greeting.size(),' ');
const string second = "* "+spaces+" *";
const string third="* "+greeting+" *";
const string first(second.size(),'*');
cout<<first<<endl;
cout<<second<<endl;
cout<<third<<endl;
cout<<second<<endl;
cout<<first<<endl;
return 0;
}
程序输出内容: