vendredi 15 novembre 2019

How to seperate HH, MM, SS, and merdian from this string in c++ "XY:56:21AM" ? Following program getting segmentation fault

Input string "XY:56:21AM". I am trying to convert this string to HH,MM,SS & meridian. I tried substr but not getting desired output. Below code getting segmentation fault.

string Conversion(string str) {

    str.erase(std::remove(str.begin(), str.end(), ':'), str.end());

    string tmp = "";
    std::vector<string> data;

   for (int i = 0; i < str.length(); ++i)
   {
      if(i < 2){
        tmp.push_back(str[i]);
      }
      if(i < 4 && i > 1){
          if(i == 2){
            tmp.push_back('\0');
            data.push_back(tmp);
            tmp = "";
          }
          tmp.push_back(str[i]);
      }
   }
   tmp.push_back('\0');
            data.push_back(tmp);
            tmp = "";
   for(auto i=data.begin(); i != data.end(); i++)
        cout << *i << endl;
}

Aucun commentaire:

Enregistrer un commentaire