Accelerated C++ 学习笔记及题解----第一章

来源:互联网 发布:网络新词汇 编辑:程序博客网 时间:2024/05/21 09:12

字符的使用

本章的主要内容是基本的格式化的输入输出.

顺带介绍的是string变量的使用.

char以及wchar_t字符按类型

string类型及其基本使用方法,

标准输入输出符

变量的定义方式.


以下是题解:

1-6

#include <iostream>#include <string>int main() {std::cout << "What is your name? ";std::string name;std::cin >> name;std::cout << "Hello, " << name<< std::endl << "And what is yours? ";std::cin >> name;std::cout << "Hello, " << name<< "; nice to meet you too!" << std::endl;return 0;}

书中的代码:

加框:

// ask for a person's name, and generate a framed greeting#include <iostream>#include <string>int main() {std::cout << "Please enter your first name: ";std::string name;std::cin >> name;// build the message that we intend to writeconst std::string greeting = "Hello, " + name + "!";// build the second and fourth lines of the outputconst std::string spaces(greeting.size(), ' ');const std::string second = "* " + spaces + " *";// build the first and fifth lines of the outputconst std::string first(second.size(), '*');// write it allstd::cout << std::endl;std::cout << first << std::endl;std::cout << second << std::endl;std::cout << "* " << greeting << " *" << std::endl;std::cout << second << std::endl;std::cout << first << std::endl;return 0;}



0 0
原创粉丝点击