-1
#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 ;
    }
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Please [post](https://stackoverflow.com/posts/48972815/edit) the the text of your question as text, not as code comments, take [the tour](https://stackoverflow.com/tour) and read [the help page](https://stackoverflow.com/help). Welcome to SO. – Ron Feb 25 '18 at 11:19
  • You can't "retype" variables. – tkausl Feb 25 '18 at 11:21
  • Thank you! I´ll definitely check it out. – Aleš Fišer Feb 25 '18 at 12:18

1 Answers1

0

The problem is that, as your compiler is saying, you are trying to use the same name (height_cm) for another variable. Try doing:

...
double height_m = height_cm/100.0;
cout << " Your height in meters is: " << height_m<< endl ;
...

This way the meter variable will have a new name and the compiler will compile. Moreover, note that I divided height_cm by 100.0 instead of 100. This is because 100 is an int and 100.0 is a float or double. If you use int then you would have an int division meaning that you would lose the decimal part.

A part from that:

  • I was thinking also of storing this value for later usage in retyping but I don´t know how to do it: The cin>>height_cm; code takes whatever the user has typed, converts it to int and stores it in a variable called height_cm that you can use anytime you want in the current function (in this case main()).
  • I´m also not sure about this, I think this assignment should be possible to get number in meters: That code would compile with no problem. However, this would end up with an int divison. If you want you can do:

Code:

...
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
...

Note that in this case although you are using 100 instead of 100.0 that would not be an int division because height_m is a double.

apalomer
  • 1,895
  • 14
  • 36