现在的位置: 首页 > 综合 > 正文

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

2019年03月03日 ⁄ 综合 ⁄ 共 994字 ⁄ 字号 评论关闭

字符的使用

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

顺带介绍的是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 write
const std::string greeting = "Hello, " + name + "!";
// build the second and fourth lines of the output
const std::string spaces(greeting.size(), ' ');
const std::string second = "* " + spaces + " *";
// build the first and fifth lines of the output
const std::string first(second.size(), '*');
// write it all
std::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;
}

抱歉!评论已关闭.