小编Lev*_*aya的帖子

使用 stoi 函数进行异常处理

我在 C++ 中的异常处理方面遇到了一些问题。我需要将数字转换为字符串,如果该字符串包含字母或超过 10 个字符,我需要给出错误。这是异常处理部分;

 while ( cin >> input )
   {
      try
      { 
         convert = castInput( input );
         cout << "The number entered was: " << convert;
      } 
      catch ( NonNumber &excp )
      {
         cout << "INVALID INPUT: " << excp.what();
      } 
      catch ( Overflow &excp )
      {
         cout << "INVALID INPUT: " << excp.what();
      } 

      cout << "\n\nPlease enter a number (end-of-file to terminate): ";
   }
Run Code Online (Sandbox Code Playgroud)

我使用 stoi 函数将字符串转换为 int 但我认为我需要打开 2 个类。我不知道为什么以及如何,因为 stoi 函数已经拥有什么功能。

c++ string exception

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

exception ×1

string ×1