使用并包含 <limits> 是一种好习惯吗?

Arn*_*aba 5 c++

我已经cin通过 using验证了输入std::numeric_limits,因此我需要包含<limits>.

这会影响我的程序的功能吗?

我在不同的论坛上阅读了很多评论,认为使用<limits>不是一个好习惯,但没有人明确说明为什么这不是一个好习惯。如果这确实是一种糟糕的做法,那么验证cin已知错误(例如无效输入长输入)的输入的好做法是什么?

#include <iostream>
#include <limits>
using namespace std;

void initMenu ();
void initDecision(int);
double areaCircle (double);
double areaRectangle (double, double);
const double PI = 4.14;
bool isValid (string);

int main()
{
    int choice;
    char cont;
    do
    {
        system ("cls");
        initMenu ();
        while (!(cin >> choice))
        {
            cin.clear();
            system ("cls");
            initMenu();
            cout << "---------------------------------------------------------------------------" << '\n';
            cout << "Invalid Input cleared and turns to [" << cin.rdstate() << "] you can try it again or go to school" << '\n';
            cout << "---------------------------------------------------------------------------" << '\n';
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        initDecision (choice);
        do
        {
            cin.clear();
            cout << "" << '\n';
            cout << "------------------------------------------------" << '\n';
            cout << "Do you want to continue using the Program? [Y/n]" << '\n';
            cout << "------------------------------------------------" << '\n';
            cin >> cont;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        while (cont != 'Y' && cont != 'y' && cont != 'N' && cont != 'n' );
        system ("cls");
        cout << "-------------------------------" << '\n';
        cout << "Thank you for using our Program" << '\n';
        cout << "-------------------------------" << '\n';
    }
    while (cont == 'Y' || cont == 'y');
    return 0;
}

void initMenu()
{
    cout << "-------------------------------------------------" << '\n';
    cout << "Choose any one option from above! [1] [2] [3] [4]" << '\n';
    cout << "-------------------------------------------------" << '\n';
    cout << "1 Circle" << '\n';
    cout << "2 Rectangle" << '\n';
}

void initDecision(int choice)
{
    double r, a, b, h;
    switch (choice)
    {
        case 1:
            cout << "Enter the Radius of a Circle: " << '\n';
            do {cin >> r;} while (!isValid("Wrong Input detected, Try again!"));
            areaCircle(r);
            break;
        case 2:
            cout << "Enter Base and Height of a Rectangle: " << '\n';
            do {cin >> b >> h;} while (!isValid("Wrong Input detected, Try again!"));
            areaRectangle(b ,h);
            break;
        default:
            cout << "----------------------------------------------------------------------" << '\n';
            cout << "The option is out of bound, Check and try again with available options" << '\n';
            cout << "----------------------------------------------------------------------" << '\n';

        break;
    }
}

double areaCircle (double r)
{
    double result = PI * r * r;
    cout << "The area of a circle that has value: [" << r << "] = [" << result << "]" << '\n';
    return result;
}

double areaRectangle (double b, double h)
{
    double result =  b * h;
    cout << "Rectangle with Base value: [" << b << "] and value of Height: [" << h << "] = [" << result << "]" << '\n';
    return result;
}

bool isValid (string error_msg)
{
    if(cin.rdstate())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max() , '\n');
        system("cls");
        initMenu();
        cout << error_msg << '\n';
        return false;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)