C++ prime plus课后习题-第二章

复习题:

1.它们叫作函数。

2.这将导致在最终的编译之前,使用iostream 文件的内容替换该编译指令。

3.它使得程序可以使用 std 名称空间中的定义。

4.cout << "Hello,world\n";

或cout<<"Hello,world"<<endl;

5.int cheeses;

6.cheeses = 32:

7.cin >> cheeses;

  1. cout << "We have " << cheeses << " varieties of cheese\n":

9.调用函数 froop()时,应提供一个参数,该参数的类型为 double,而该函数将返回一个int值。例如,可以像下面这样使用它:

int gval =froop(3.14159);

函数 rattle()接受一个int参数且没有返回值。例如,可以这样使用它:

rattle(37):

函数prune()不接受任何参数且返回一个int值。例如,可以这样使用它:

ntresidue=prune();

10.当函数的返回类型为空时,不用在函数中使用返回值。然而,如果不提供返回值,则可以使用它:

return;

11.如果编译器指出 `cout` 是一个未知标识符,这通常意味着编译器没有识别到 `cout` 所依赖的库或者命名空间。在 C++ 中,`cout` 是标准库中的一个对象,用于输出数据到标准输出流(通常是屏幕)。以下是导致这种问题可能的原因,以及三种修复方法:

可能的原因:

  1. **未包含必要的头文件**:`cout` 定义在 `<iostream>` 头文件中,如果未包含这个头文件,编译器将无法识别 `cout`。

  2. **命名空间问题**:如果使用了 `using namespace std;` 或者没有正确地使用 `std::` 前缀,可能会导致命名空间相关的问题。

  3. **编译器配置问题**:在某些情况下,如果编译器配置不正确,或者使用的编译器不支持 C++ 标准库,也可能导致这个问题。

修复方法:

  1. **包含 `<iostream>` 头文件**:
cpp 复制代码
   #include <iostream>
   int main() {
       std::cout << "Please enter your PIN:" << std::endl;
       return 0;
   }

确保在代码的顶部包含了 `<iostream>` 头文件。

  1. **使用 `std::` 前缀**:
cpp 复制代码
   int main() {
       std::cout << "Please enter your PIN:" << std::endl;
       return 0;
   }

在 `cout` 前使用 `std::` 前缀,明确指出 `cout` 来自 `std` 命名空间。

  1. **添加 `using namespace std;`**:
cpp 复制代码
   #include <iostream>
   using namespace std;
   int main() {
       cout << "Please enter your PIN:" << endl;
       return 0;
   }

在包含 `<iostream>` 头文件后,添加 `using namespace std;` 声明,这样你就可以在不使用 `std::` 前缀的情况下使用 `cout`。

确保选择适合你代码风格和项目要求的修复方法。通常,推荐使用 `std::` 前缀,因为它可以避免命名冲突,并且使代码更加清晰。

编程练习答案:

cpp 复制代码
#include <iostream>
#include <string>

int main() {
    // 定义姓名和地址变量
    std::string name = "张三";
    std::string address = "中国北京市海淀区中关村大街1号";

    // 显示姓名和地址
    std::cout << "姓名: " << name << std::endl;
    std::cout << "地址: " << address << std::endl;

    return 0;
}
cpp 复制代码
#include <iostream>
int main(){
	long meters;
	double yards;
	
	std::cout << "please enter a distance in meters:";
	std::cin >> meters;
	yards = meters * 3.28084;
	std::cout << "the distance in yards is:" << yards <<"yards"<<std::endl;
	return 0;
}
cpp 复制代码
#include <iostream>

void printFirstLine();
void printSecondLine();
void printThirdLine();

void printFirstLine() {
	std::cout << "Three blind mice" << std::endl;
}

void printSecondLine(){
	std::cout << "Three blind mice" << std::endl;
}

void printThirdLine(){
	std::cout << "See how they run" << std::endl;
}
int main(){
	printFirstLine();
	printSecondLine();
	printThirdLine();
	printThirdLine();
}
cpp 复制代码
#include <iostream>
int main(){
	int age;
	int months;
	
	std::cout << "Enter your age:";
	std::cin >> age;
	
	months = age * 12;
	
	std::cout << "your age in months is:" << months<< std::endl;
	return 0;
}
cpp 复制代码
#include <iostream>

// 函数声明
double celsiusToFahrenheit(double celsius);

int main() {
    double celsius, fahrenheit;

    std::cout << "Please enter a Celsius value: ";
    std::cin >> celsius;

    // 调用函数转换摄氏温度到华氏温度
    fahrenheit = celsiusToFahrenheit(celsius);

    // 显示结果
    std::cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit." << std::endl;

    return 0;
}

// 用户定义的函数,将摄氏温度转换为华氏温度
double celsiusToFahrenheit(double celsius) {
    return 1.8 * celsius + 32.0;
}
cpp 复制代码
#include <iostream>

// 函数声明
double lightYearsToAstronomicalUnits(double lightYears);

int main() {
    double lightYears, astronomicalUnits;

    std::cout << "Enter the number of lightyears: ";
    std::cin >> lightYears;

    // 调用函数转换光年到天文单位
    astronomicalUnits = lightYearsToAstronomicalUnits(lightYears);

    // 显示结果
    std::cout << lightYears << " light years = " << astronomicalUnits << " astronomical units." << std::endl;

    return 0;
}

// 用户定义的函数,将光年转换为天文单位
double lightYearsToAstronomicalUnits(double lightYears) {
    const double CONVERSION_FACTOR = 63240.0; // 1光年等于63240天文单位
    return lightYears * CONVERSION_FACTOR;
}
cpp 复制代码
#include <iostream>

void displayTime(int hours,int minutes);

int main(){
	int hours,minutes;
	
	std::cout <<"Enter the number of hours:";
	std::cin >> hours;
	std::cout << "Enter the number of minutes:";
	std::cin >> minutes;
	
	displayTime(hours,minutes);
	
	return 0;
}

void displayTime(int hours,int minutes){
	std::cout << "Time:" << hours << ":" << minutes << std::endl;
}
相关推荐
o独酌o18 分钟前
递归的‘浅’理解
java·开发语言
Book_熬夜!20 分钟前
Python基础(六)——PyEcharts数据可视化初级版
开发语言·python·信息可视化·echarts·数据可视化
m0_631270401 小时前
高级c语言(五)
c语言·开发语言
Lenyiin1 小时前
《 C++ 修炼全景指南:十 》自平衡的艺术:深入了解 AVL 树的核心原理与实现
数据结构·c++·stl
2401_858286111 小时前
53.【C语言】 字符函数和字符串函数(strcmp函数)
c语言·开发语言
程序猿进阶1 小时前
如何在 Visual Studio Code 中反编译具有正确行号的 Java 类?
java·ide·vscode·算法·面试·职场和发展·架构
Eloudy1 小时前
一个编写最快,运行很慢的 cuda gemm kernel, 占位 kernel
算法
程序猿练习生1 小时前
C++速通LeetCode中等第5题-无重复字符的最长字串
开发语言·c++·leetcode
2401_858120262 小时前
MATLAB中的无线通信系统部署和优化工具有哪些
开发语言·matlab
MATLAB滤波2 小时前
【PSINS】基于PSINS工具箱的EKF+UKF对比程序|三维定位|组合导航|MATLAB
开发语言·matlab