Debug Error -Abort() Has Been Called

瘦欲@ 提交于 2019-12-03 11:26:28

There are couple of issues:

  1. When you call superLucky from main, s is empty. stoi(s) throws an exception when s is empty.

  2. The check s.size() > 10 is not robust. It is platform dependent. You can use a try/catch block to deal with it instead of hard coding a size.

Here's a more robust version of the function.

void superLucky(int n,string s, int count4, int count7)
{
   int d = 0;
   if ( s.size() > 0 )
   {
      try
      {
         d = stoi(s);
      }
      catch (...)
      {
         return;
      }

      if (( d >= n ) && (count4 == count7) && (count4+count7)!=0)
      {
         cout << s << endl;
         return;
      }
   }

   superLucky(n, s + '7',count4,count7+1);
   superLucky(n, s + '4', count4+1, count7);
}

It's probably because stoi() have thrown an invalid_argument exception.

On the first call to superLucky, you pass an empty string to std::stoi. When unable to perform the conversion, stoi throws an exception. That exception is not caught, so uncaught_exception gets called, which in turn calls abort

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!