如何在没有打印语句的情况下报告用户输入错误?

时间:2018-07-27 20:00:49

标签: c++ error-handling

这是我的C ++函数。如果左<0 ||我应该报告用户输入错误。右<0 ||顶部<0 ||底部<0 ||高度<= 0 ||宽度<= 0 ||高度> 1200 ||宽度> 1600 ||底部> 1200 || right>1600。但是,我不能使用任何打印语句。我还能如何报告用户错误?

注意:此代码是使用检测到的脸部坐标确定人距相机的距离范围的功能。

#include "stdafx.h"
#include <iostream>
using namespace std;

void getDistance(int left, int right, int top, int bottom, bool isCNN, double& minDistance, double& maxDistance)
{

    int height = bottom - top;
    int width = right - left;
    if (left < 0 || right < 0 || top < 0 || bottom < 0 || height <= 0 || width <= 0 || height > 1200 || width > 1600 || bottom > 1200 || right > 1600)
    {

    }
    else if (isCNN)
    {
        if (width >= 215 && height >= 281)
            minDistance = 0, maxDistance = 14;
        else if (width >= 124 && height >= 167)
            minDistance = 14, maxDistance = 33.75;
        else if (width >= 76 && height >= 111)
            minDistance = 33.75, maxDistance = 53.5;
        else if (width >= 56 && height >= 94)
            minDistance = 53.5, maxDistance = 73.25;
        else if (width >= 49 && height >= 84)
            minDistance = 73.25, maxDistance = 93;
        else if (width >= 41 && height >= 71)
            minDistance = 93, maxDistance = 112.75;
        else if (width >= 28 && height >= 57)
            minDistance = 112.75, maxDistance = 172;
        else if (width >= 23 && height >= 49)
            minDistance = 172, maxDistance = 270.75;
        else
            minDistance = 270.75, maxDistance = 480;
    }
    else
    {
        if (width >= 330)
            minDistance = 0, maxDistance = 6.375;
        else if (width >= 238)
            minDistance = 6.375, maxDistance = 16.25;
        else if (width >= 168)
            minDistance = 16.25, maxDistance = 26.125;
        else if (width >= 122)
            minDistance = 26.125, maxDistance = 36;
        else if (width >= 108)
            minDistance = 36, maxDistance = 55.75;
        else if (width >= 91)
            minDistance = 55.75, maxDistance = 75.5;
        else
            minDistance = 75.5, maxDistance = 144;
    }
}
int main()
{
    double min = NULL;
    double max= NULL;
    getDistance(-766, 981, 328, 609, false, min, max);
        cout << "Minimum distance is " << min << " inches\n";
        cout << "Maximum distance is " << max << " inches\n";
    return 0;
}

3 个答案:

答案 0 :(得分:1)

以下方法已在现实世界中全部使用。

  1. 将返回类型更改为bool或int,并返回一个表示错误的值。
  2. 引发异常。
  3. 安静地返回而不会破坏任何东西,但是也不会做任何事情。
  4. 发出恼人的哔哔声,否则不知道出什么问题了。
  5. 尝试做到最好-通过使用安全的默认值,或将有问题的值限制在所需范围内。
  6. 将错误记录到文件中。

您可以根据实际情况选择类似的方法。在决定如何处理错误时,您还需要仔细考虑它们是否实际上是用户错误,逻辑错误或系统错误。

答案 1 :(得分:0)

您声明要“不使用打印语句就报告错误”,但是如果这仅表示您不想使用cout,则可以使用cerrcerr用于错误输出。

cerr << "Error message";

或者,您可以将错误消息写入文件:

ofstream file;
file.open("error_log.txt");
file << "Error message\n";
file.close();

答案 2 :(得分:0)

您已将此帖子标记为C ++。

  

如果“ left <0 || ... || right> 1600”,我应该报告用户输入错误。   但是,我不能使用任何打印语句。我还能如何报告用户错误?

Linux理念的一小部分是成功不会引起“噪音”的想法。

在不使用print,printf或cout的情况下,我们仍然可以导致“失败的噪音”

#include <iostream>
#include <limits>  // max and min double
#include <cassert>

class T651_t
{
public:

   T651_t() = default;
   ~T651_t() = default;

   int operator()() { return exec();  }

private: // methods

   int exec()
      {
       //double min = std::numeric_limits<double>::min();        // 2.22507e-308 units - lowest  pos value
         double min = std::numeric_limits<double>::max() * -1.0; // 1.79769e+308 units - lowest  neg value
         double max = std::numeric_limits<double>::max();        // 1.79769e+308 units - highest pos value

         std::cout << "\n  Min (lowest   neg) double is " << min << " units";
         std::cout << "\n  Max (hightest pos) double is  " << max << " units";

         getDistance(-766, 981, 328, 609, false, min, max);

         std::cout << "\n  Minimum distance is " << min << " units";
         std::cout << "\n  Maximum distance is  " << max << " units"; 
         std::cout << std::endl;

         return 0;
      }

   void getDistance(int left, int right, int top, int bottom, bool isCNN,
                    double& minDistance, double& maxDistance)
      {
         int height = bottom - top;
         int width  = right  - left;

         if(false) // diagnostic ONLY
           std::cout << "\n  >>>" << left
                     << "  "   << right
                     << "  "   << top
                     << "  "   << bottom
                     << "  "   << minDistance
                     << "  "   << maxDistance
                     << "  "   << isCNN
                     << "  "   << height
                     << "  "   << width
                     << "<<<" << std::endl;
         {
            // how to display "invalid input error" with no print nor cout
            // assert is a failure noise, the line number
            //         reported identifies which failure
            assert ( !(left < 0) );      
            assert ( !(right < 0) );
            assert ( !(top < 0) );
            assert ( !(bottom < 0) );
            assert ( !(height <= 0) );
            assert ( !(width <= 0) );
            assert ( !(height > 1200) );
            assert ( !(width > 1600) );
            assert ( !(bottom > 1200) );
            assert ( !(right > 1600) );
         }
         if (isCNN)
         {
           // ...
         }
         else  // !isCNN
         {
            // ...
         }
      } // void getDistance(...)

}; // class T651_t


int main(int, char* *) { return T651_t()(); }

输出:

R6xx: dumy651.cc
rm -f dumy651
g++ -m64  -O3 -ggdb -std=c++17 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -Werror=vla -Wcast-align -Wcast-qual -Wconversion -Wpointer-arith -Wunused -Wold-style-cast -Woverloaded-virtual   -O0   dumy651.cc  -o dumy651  -L../../bag -lbag_i686 -lposix_i686 -lrt -pthread

real    0m1.149s
user    0m0.996s
sys 0m0.123s

  Min (lowest   neg) double is -1.79769e+308 units
  Max (hightest pos) double is  1.79769e+308 units
dumy651: dumy651.cc:63: void T651_t::getDistance(int, int, int, int, bool, double&, double&): Assertion `!(left < 0)' failed.
/bin/bash: line 1:  5151 Aborted                 (core dumped) ./dumy651

第63行是“断言(!(left <0));”

注意:类T651_t是函子。