c++中的Double函数
Double Function in C++
我被第3.3章c++的原理和实践卡住了:
他提到:-让"姓名和年龄"示例运行。然后,将其修改为以月为单位写出年龄:以年为单位读取输入,然后(使用*运算符)乘以12。把年龄读成a两倍是为了让孩子们为自己五岁半感到骄傲而不是只有五个。
下面是我运行的没有问题的姓名和年龄示例:
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter your name and age.n";
string name; //string variable
int age; //integer variable
cin >> name >> age; //reads string and integer
cout << "Hello, " << name << " (Age: " << age << ")n";
}
之后,我尝试将年龄改为月,我可以通过使用以下命令来实现:
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter your name and agen";
string name;
int age;
cin >> name >> age;
cout << "Hello, " << name << " (Age: " << age * 12 << " Months Old)n";
}
所以问题仍然是,我如何"将年龄读入双精度体"?尽管我设法让我的输出以月为单位显示年龄,但我认为我还没有清楚地理解处理这个问题的方法。
仅使用double
作为读取数据的变量类型。
// What is this?
//#include "std_lib_facilities.h"
#include <iostream>
#include <string>
using std::cout;
using std::string;
using std::cin;
int main()
{
cout << "Please enter your name and agen";
string name;
double age;
cin >> name >> age;
cout << "Hello, " << name << " (Age: " << age * 12 << " Months Old)n";
return 0;
}
直接声明age为double。你的问题应该得到解决。
样例代码#include "std_lib_facilities.h"
int main()
{
cout << "Please enter your name and agen";
string name;
double age;
cin >> name >> age;
cout << "Hello, " << name << " (Age: " << age * 12 << " Months Old)n";
}
现在程序应该可以正常运行了
相关文章:
- C++ 模板函数无法将"std:string"转换为"double"作为回报
- emplace_back和push_back给出错误'double free or corruption (fasttop)'尽管定义了复制和移动构造函数
- 调用的对象类型 'double' 不是 report() 函数的函数或函数指针
- C++ 调用析构函数后动态模板队列"double free or corruption (out)"
- 重载函数 int vs double
- 将双指针作为参数传递给需要引用 std::vector <double>的函数
- 将 std::shared_ptr<<std::vector<double>> 传递给函数
- pybind11:如何包装以 std::vector<double> 为参数以避免数据复制的 C++ 函数
- 用于检查"is int"/"is double"/等的模板函数
- 如何为包含map<int,*double>成员的类编写析构函数?
- 我可以为向量函数返回 NULL<double> 吗?
- 如何在类/类型中添加更多函数来构建 int、double、std::string 等
- 错误:调用'begin(long double [nPoints])'没有匹配函数;使用硬编码的 int 与整数变量初始化向量
- "double(*)(string, double, double)"尝试传递函数C++时"double"转换错误
- C++:无法使用条件类型在模板函数中使用 'double' 类型的 lvalue 初始化 'char*' 类型的参数
- SWIG - 如何使用采用'const double*'的构造函数将类包装为 python
- 重载的函数调用double不明确
- 有没有办法<int><double>在使用 C++11 函数调用期间自动将"向量"提升为"向量"?
- 错误:没有用于初始化'List<double>::node'的匹配构造函数
- C++ - 为 std::vector 创建新的构造函数<double>?