从厘米到C 米,将INT递增至状态高度

Retyping int to double to state height from centimeters to meters c++

本文关键字:状态 高度 INT      更新时间:2023-10-16
#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。这是因为100int,而100.0floatdouble。如果您使用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_mdouble