jeudi 30 mars 2017

Input validation to only accept numbers

I'm creating program and it should only be allowed to accept numbers if a letter is entered it should re-ask the use to enter a number. When i run the code i get an error saying there's a problem with the break heres a copy of my code

#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; 

}

Here's the error im receiving

Aucun commentaire:

Enregistrer un commentaire