[Essential C++ notes] Chapter 1 配套练习题

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 <<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值