'c++ different behavior of a float variable in function CallBackFunc (Opencv)

std::string temp="22.7";
float a  = std::stof(temp);
cout<< "a:"<< a <<endl;

Hi to everyone, i'm a beginner in c++. I've these lines in an opencv function called CallBackFunc(). If O print the var a from the CallBackFunc(), I obtain "a:22" as output while in the main() these 3 lines gives me the expected output "a:22.7".

How is it possible? I don't really understand. I've tried on different IDE so I think that there is something wrong.

I've tried lot of things but...

I expect that even in CallBackFunc the output will be "a:22.7"

#include <string>
#include <iostream> 
#include "opencv2/highgui.hpp"




void CallBackFunc(int event, int x, int y,  int flags, void* userdata)
{
    /////////////////////////////HERE THE OUTPUTIS "a:22"
    std::string temp="22.7";
    double c_a  = std::stod(temp);
    cout<< "TAKE POINT INFOOO"<< c_a <<endl;
}

int main(int argc, char** argv)
{
    namedWindow("Webcam", WINDOW_FULLSCREEN); //migliore fin ora: WINDOW_AUTOSIZE
    createTrackbar( "track1", "Webcam", NULL, 1,  NULL);

    ////////////////////////////HERE THE OUTPUTIS "a:22.7"
    std::string temp="22.7";
    double c_a  = std::stod(temp);
    cout<< "TAKE POINT INFOOO"<< c_a <<endl;
   

   
    while(true)
    {
        
        setMouseCallback("Webcam", CallBackFunc, NULL);
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source