Search code examples
c++type-conversionstatic-cast

I convert Int to Float but the Id-Type doesn't change


I convert int to float and input 1.23 to 'a' but output is 1 what is wrong?

int a = 123;
static_cast<float>(a);
cout << typeid(a).name(); //int

cin >> a; //1.23
cout << a;  //1


return 0;

Solution

  • You have to asign the return value to a variable of the preferred type:

    float result = static_cast<float>(your_var);