1.1 在系统中编译并执行程序
#include <iostream>
#include <string>
using namespace std;
int main(){
string user_name;
cout << "Please enter your first name: ";
cin >> user_name;
cout << endl << "Hello, " << user_name << " ... and goodbye!" << endl;
return 0;
}
1.2 将string头文件注释掉,重新编译程序,会发生什么?
// #include <string>
程序能够编译并执行
取消对string头文件注释,注释下一行,会发生什么?
// using namespace std;
编译不通过,错误信息之一如下,由于string、cin、cout属于std命名空间,若不声明则无法使用
D:\CLion-project\Essential\chapter1\main.cpp: In function 'int main()':
D:\CLion-project\Essential\chapter1\main.cpp:5:5: error: 'string' was not declared in this scope
string user_name;
^~~~~~
1.3 将函数名main()改为my_main(),重新编译将有什么后果?
undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
C++将从main()函数开始编译执行,若没有定义main()函数将编译出错
1.4 扩充此程序的内容,要求用户同时输入名字和姓氏,修改输出结果,同时打印姓氏和名字
#include <iostream>
#include <string>
using namespace std;
int main(){
string user_firstname, user_lastname;
cout << "Please enter your first name and last name: ";
cin >> user_firstname >> user_lastname;
cout << endl <<