c++ converting string to int

前端 未结 4 619
我在风中等你
我在风中等你 2021-01-28 06:55
//sLine is the string
for(int l = 0; l < sLine.length(); l++)
{
    string sNumber;
    if(sLine[l] == \'-\')
    {   
        sNumber.push_back(sLine[l]);
        sN         


        
4条回答
  •  余生分开走
    2021-01-28 07:36

    It's displaying a zero for all nonrecognized characters, because atoi returns 0 when given a non-numeric string (like a space!)

    However, what you want to do, is shockingly simple:

    std::stringstream ss(sLine);
    int num;
    while(ss >> num) {
        cout << num;
    }
    

提交回复
热议问题