C++ 输入验证以仅接受数字

C++ 输入验证以仅接受数字,c++,validation,c++11,C++,Validation,C++11,我正在创建一个程序,它应该只允许接受数字,如果输入了一个字母,它应该重新要求用户输入一个数字。当我运行代码时,我得到一个错误,说中断有问题。这是我的代码副本 #include <iostream> using namespace std; class Triangle { private: double base; double height; public: void setBase(double);

我正在创建一个程序,它应该只允许接受数字,如果输入了一个字母,它应该重新要求用户输入一个数字。当我运行代码时,我得到一个错误,说中断有问题。这是我的代码副本

#include <iostream>
using namespace std;

class Triangle
{
    private:
        double base;
        double height;
    public:
        void setBase(double);
        void setHeight(double);
        double getBase() const;
        double getHeight() const;
        double getArea() const;
};

void Triangle::setBase(double b)
{
    base = b;
}

void Triangle::setHeight(double hg)
{
    height = hg;
}

double Triangle::getBase() const
{
    return base;
}

ouble Triangle::getHeight() const
{
    return height;
}

double Triangle::getArea() const
{
    return .50 *(base * height);
}

int main()
{
    double number; 
    double totalArea;
    Triangle one; 
    Triangle two;
    Triangle three;

    do
    {
        cout << "What is the base of Triangle One:"; 
        cin >> number;

        one.setBase(number); 
            cout << "You have entered " << number << " for the base of the triangle.\n";

        cout << "What is the height of Triangle One:"; 
        cin >> number;
        one.setHeight(number); 
            cout << "You have entered " << number << " for the height of the triangle.\n";

            cout << "What is the base of Triangle Two: "; 
        cin >> number;
        two.setBase(number); 
            cout << "You have entered " << number << " for the base of the triangle.\n";

        cout << "What is the height of Triangle Two:"; 
        cin >> number;
        two.setHeight(number); 
            cout << "You have entered " << number << " for the height of the triangle.\n";

            cout << "What is the base of Triangle Three:"; 
        cin >> number;
        three.setBase(number); 
            cout << "You have entered " << number << " for the base of the triangle.\n";

        cout << "What is the height of Triangle Three:"; 
        cin >> number;
        three.setHeight(number); 
             cout << "You have entered " << number << " for the height of the triangle.\n";
    }   
    while (0); 
    {
        if (cin >> number) 
        {
            break;
        } 
        else 
        {
            cout << "Invalid Input! Please input a numerical value." << endl;
                    cin.clear();
            while (cin.get() != '\n') ; 
        }
    }


    totalArea = one.getArea() + two.getArea() + three.getArea(); 

    cout << "The total area of the three triangles is " << totalArea << endl; 

    return 0; 

}
#包括
使用名称空间std;
阶级三角
{
私人:
双基;
双高;
公众:
无效挫折(双);
空隙设置高度(双倍);
双getBase()常量;
双getHeight()常量;
双getArea()常量;
};
空心三角形::收进(双b)
{
基数=b;
}
空心三角形::设置高度(双汞柱)
{
高度=汞柱;
}
双三角形::getBase()常量
{
返回基地;
}
双三角形::getHeight()常量
{
返回高度;
}
双三角形::getArea()常量
{
返回.50*(基准*高度);
}
int main()
{
双数;
双总面积;
三角形一;
三角形二;
三角形三;
做
{
数量;
一、挫折(数);

cout您在双on中缺少d:

ouble Triangle::getHeight() const
{
    return height;
}

执行while语法应如下所示:

do{

}while(x == 0);
do{

}while();
{ <--- //this

} <--- //and this
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;

//Work-Class 
class Triangle {
private:
    double base;
    double height;

public:
    void setBase(double);
    void setHeight(double);
    double getBase() const;
    double getHeight() const;
    double getArea() const;
};

void Triangle::setBase(double b) {
    base = b;
}

void Triangle::setHeight(double hg) {
    height = hg;
}

double Triangle::getBase()const {
    return base;
}

double Triangle::getHeight() const {
    return height;
}

double Triangle::getArea() const {
    return (base*height) / 2;
}

//conversion and test function
double getDoubleString(string questString) {
    string userAnswer;
    double d;

    cout << questString;
    getline(cin, userAnswer);
    try {
        d = stod(userAnswer);
    }
    catch (const std::invalid_argument& ia) {
        cerr << "Invalid argument: " << ia.what() << '\n';
        d = 0.0;
    }
    return d;
}

//Enter the hell
int main() {
    double number;
    double totalArea;
    Triangle one;
    Triangle two;
    Triangle three;

    cout << "--------------------------------------------------------------\n\n";
    //Triangle one
    one.setBase(getDoubleString("What is the base of Triangle One: "));
    cout << "You have entered " << one.getBase() << " for the base of the triangle.\n";
    one.setHeight(getDoubleString("What is the height of Triangle One: "));
    cout << "You have entered " << one.getHeight() << " for the height of the triangle.\n\n";

    //Triangle two
    two.setBase(getDoubleString("What is the base of Triangle Two: "));
    cout << "You have entered " << two.getBase() << " for the base of the triangle.\n";
    two.setHeight(getDoubleString("What is the height of Triangle Two: "));
    cout << "You have entered " << two.getHeight() << " for the height of the triangle.\n\n";

    //Triangle three
    three.setBase(getDoubleString("What is the base of Triangle Three: "));
    cout << "You have entered " << three.getBase() << " for the base of the triangle.\n";
    three.setHeight(getDoubleString("What is the height of Triangle Three: "));
    cout << "You have entered " << three.getHeight() << " for the height of the triangle.\n\n";

    totalArea = one.getArea() + two.getArea() + three.getArea();

    cout << "--------------------------------------------------------------\n\n";
    cout << "The total area of the three triangles ist " << totalArea << endl;
    return 0;
}
你是怎么做到的

do{

}while(x == 0);
do{

}while();
{ <--- //this

} <--- //and this
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;

//Work-Class 
class Triangle {
private:
    double base;
    double height;

public:
    void setBase(double);
    void setHeight(double);
    double getBase() const;
    double getHeight() const;
    double getArea() const;
};

void Triangle::setBase(double b) {
    base = b;
}

void Triangle::setHeight(double hg) {
    height = hg;
}

double Triangle::getBase()const {
    return base;
}

double Triangle::getHeight() const {
    return height;
}

double Triangle::getArea() const {
    return (base*height) / 2;
}

//conversion and test function
double getDoubleString(string questString) {
    string userAnswer;
    double d;

    cout << questString;
    getline(cin, userAnswer);
    try {
        d = stod(userAnswer);
    }
    catch (const std::invalid_argument& ia) {
        cerr << "Invalid argument: " << ia.what() << '\n';
        d = 0.0;
    }
    return d;
}

//Enter the hell
int main() {
    double number;
    double totalArea;
    Triangle one;
    Triangle two;
    Triangle three;

    cout << "--------------------------------------------------------------\n\n";
    //Triangle one
    one.setBase(getDoubleString("What is the base of Triangle One: "));
    cout << "You have entered " << one.getBase() << " for the base of the triangle.\n";
    one.setHeight(getDoubleString("What is the height of Triangle One: "));
    cout << "You have entered " << one.getHeight() << " for the height of the triangle.\n\n";

    //Triangle two
    two.setBase(getDoubleString("What is the base of Triangle Two: "));
    cout << "You have entered " << two.getBase() << " for the base of the triangle.\n";
    two.setHeight(getDoubleString("What is the height of Triangle Two: "));
    cout << "You have entered " << two.getHeight() << " for the height of the triangle.\n\n";

    //Triangle three
    three.setBase(getDoubleString("What is the base of Triangle Three: "));
    cout << "You have entered " << three.getBase() << " for the base of the triangle.\n";
    three.setHeight(getDoubleString("What is the height of Triangle Three: "));
    cout << "You have entered " << three.getHeight() << " for the height of the triangle.\n\n";

    totalArea = one.getArea() + two.getArea() + three.getArea();

    cout << "--------------------------------------------------------------\n\n";
    cout << "The total area of the three triangles ist " << totalArea << endl;
    return 0;
}
do{
}while();
{ 
  • 做{ ... }而(0)
  • 意味着您的循环只执行一次。它将始终退出。因此,您可以将此do while循环保留下来

  • while(0)之后的代码;看起来像是测试用户输入的函数
  • 我将介绍字符串和异常,并这样编写:

    do{
    
    }while(x == 0);
    
    do{
    
    }while();
    { <--- //this
    
    } <--- //and this
    
    #include <iostream>
    #include <stdexcept>
    #include <string>
    using namespace std;
    
    //Work-Class 
    class Triangle {
    private:
        double base;
        double height;
    
    public:
        void setBase(double);
        void setHeight(double);
        double getBase() const;
        double getHeight() const;
        double getArea() const;
    };
    
    void Triangle::setBase(double b) {
        base = b;
    }
    
    void Triangle::setHeight(double hg) {
        height = hg;
    }
    
    double Triangle::getBase()const {
        return base;
    }
    
    double Triangle::getHeight() const {
        return height;
    }
    
    double Triangle::getArea() const {
        return (base*height) / 2;
    }
    
    //conversion and test function
    double getDoubleString(string questString) {
        string userAnswer;
        double d;
    
        cout << questString;
        getline(cin, userAnswer);
        try {
            d = stod(userAnswer);
        }
        catch (const std::invalid_argument& ia) {
            cerr << "Invalid argument: " << ia.what() << '\n';
            d = 0.0;
        }
        return d;
    }
    
    //Enter the hell
    int main() {
        double number;
        double totalArea;
        Triangle one;
        Triangle two;
        Triangle three;
    
        cout << "--------------------------------------------------------------\n\n";
        //Triangle one
        one.setBase(getDoubleString("What is the base of Triangle One: "));
        cout << "You have entered " << one.getBase() << " for the base of the triangle.\n";
        one.setHeight(getDoubleString("What is the height of Triangle One: "));
        cout << "You have entered " << one.getHeight() << " for the height of the triangle.\n\n";
    
        //Triangle two
        two.setBase(getDoubleString("What is the base of Triangle Two: "));
        cout << "You have entered " << two.getBase() << " for the base of the triangle.\n";
        two.setHeight(getDoubleString("What is the height of Triangle Two: "));
        cout << "You have entered " << two.getHeight() << " for the height of the triangle.\n\n";
    
        //Triangle three
        three.setBase(getDoubleString("What is the base of Triangle Three: "));
        cout << "You have entered " << three.getBase() << " for the base of the triangle.\n";
        three.setHeight(getDoubleString("What is the height of Triangle Three: "));
        cout << "You have entered " << three.getHeight() << " for the height of the triangle.\n\n";
    
        totalArea = one.getArea() + two.getArea() + three.getArea();
    
        cout << "--------------------------------------------------------------\n\n";
        cout << "The total area of the three triangles ist " << totalArea << endl;
        return 0;
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    //工人阶级
    阶级三角{
    私人:
    双基;
    双高;
    公众:
    无效挫折(双);
    空隙设置高度(双倍);
    双getBase()常量;
    双getHeight()常量;
    双getArea()常量;
    };
    空心三角形::收进(双b){
    基数=b;
    }
    空心三角形::设置高度(双汞柱){
    高度=汞柱;
    }
    双三角形::getBase()常量{
    返回基地;
    }
    双三角形::getHeight()常量{
    返回高度;
    }
    双三角形::getArea()常量{
    返回(基准*高度)/2;
    }
    //转换和测试功能
    double getDoubleString(字符串questString){
    字符串userAnswer;
    双d;
    
    无法显示准确的错误消息。
    while(0);
    不正确。请删除
    。而且
    0
    被视为
    false
    ,因此将永远不会进入此循环(即使您修复了
    )@TatsuyukiIshi我已经添加了错误message@WhozCraig我将0改为1,并删除了;我仍然收到相同的errorObservant,但与问题无关。抱歉,这里的代码是如何输入的,但我有d@WhozCraig这确实与问题有关。海报的主要目的是不接收t中的错误继承人程序。虽然正确,但这不会纠正无需连接循环或开关的
    中断问题。