简体   繁体   中英

Why debugging and running outputs are different in C++?

I want to read ".text" file and convert string as double. In debug mode i can read text as 99,03 but in running mode i can read only 99. I can't understand how it would be? My converting code is below

   double a = std::stod(text)

std::stod is affected by locale.

#include <iostream>
#include <string>
#include <clocale>

int main(void){
    std::setlocale(LC_ALL, "C");

    std::string s{"99.03"}; 
    double d = std::stod( s );
    std::cout<< d << "\n";
    std::setlocale(LC_ALL, "de_DE.UTF-8"); // A locale installed on your machine.
    std::string s2{"99,03"};
    double d2 = std::stod( s2 );
    std::cout<< d2 << "\n";
}

You may get the result as following: ( tested on msvc and gcc 9.3.0 )

99.03
99.03

If you want std::cout also print comma as decimal separator, imbue is required. You may refer to this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM