从厘米到C 米,将INT递增至状态高度
Retyping int to double to state height from centimeters to meters c++
#include <iostream> // try to convert height in centimeters to meters
using namespace std;
int main()
{
int height_cm ; // declaring first int in centimeters
cout << " State your height in centimeters: " << endl ; // stating
cin >> height_cm ; /* I was thinking also of storing this value for later reusage in retyping but I don´t know how to do it */
double (height_cm) ; /* here is the problem,debugger says that I can not declare height_cm again because I had declared it as int before , but I´m actually trying to retype it */
height_cm /= 100 ; /* I´m also not sure about this , I think this assignment should be possible to get number in meters */
cout << " Your height in meters is: " << height_cm << endl ; /* this should give me height in meters */
return 0 ;
}
问题是,正如您的编译器所说的那样,您正在尝试使用相同的名称(height_cm(作为另一个变量。尝试做:
...
double height_m = height_cm/100.0;
cout << " Your height in meters is: " << height_m<< endl ;
...
这样,仪表变量将具有新名称,并且编译器将编译。此外,请注意,我将height_cm
除以100.0
而不是100
。这是因为100
是int
,而100.0
是float
或double
。如果您使用int
,那么您将有一个int
分区,这意味着您将失去小数。
一个部分:
- 我也在考虑存储此值以供以后的播种中使用,但我不知道该怎么做:
cin>>height_cm;
代码将用户输入的任何内容,将其转换为int
和商店它在一个称为height_cm
的变量中,您可以在当前函数中随时使用(在这种情况下为main()
(。 - 我也不确定这一点,我认为该任务应该以米为单位:该代码不会毫无问题地编译。但是,这最终会带有
int
Divison。如果愿意,可以做:
代码:
...
double height_m(height_cm);// this converts the int to double and stores it in the new variable height_m
height_m /= 100;// Divide the double variable height_m by 100 and store it again in height_m
...
请注意,在这种情况下,尽管您使用的是100
,而不是100.0
,这不是int
分区,因为height_m
是double
。
相关文章:
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- OSX MetalKit CVMetalTextureCacheCreateTextureFromImage失败,状态:
- std::future_error:无关联状态
- 如何避免LED在循环状态变化中闪烁?
- boost 是否有按特殊类型值编码状态"compact optional"?
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 当可输入框在窗口中处于活动状态时获得通知的任何方法
- C++:如何根据地形高度更新玩家身高?
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 如何更改QComboBox项目的高度大小?
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 试图在崇高中奔跑. 错误 已发生: 收集2.exe: 错误: ld 返回 1 退出状态
- 更改高度贴图,使其在 4x4 网格上显示 16 个 hieghtmap
- 我试图用这段代码找到二叉树的高度,但它一直返回 0,有人可以告诉我为什么吗?
- 从厘米到C 米,将INT递增至状态高度