简体   繁体   中英

Using boost::lexical_cast<double>(string)

I am a beginner and need an explanation as to why this doesn't work:

string myArray[620223];
//myArray is then populated with a while loop using ifstream and getline
string myString = myArray[9];
double myValue;

try {
    myValue = boost::lexical_cast<double>(myString);
} 
catch(boost::bad_lexical_cast const&) {
    myValue = 0;
}

myArray[9] is non-zero but the resulting double is always zero. Any help would be greatly appreciated. Thank you.

Since the code appears fine, here's an educated guess. myArray[9] is the tenth array element. myArray[0] is the first. You actually wanted the ninth element, myArray[8] .

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