I wrote a program that calculates cylinder and rectangle areas with user inputs. But I want improve my code. When user enter a character instead of number, it should output an error message. So I wrote some extra code but it's not working correctly.
#include <iostream>
#include <ctype.h>
using namespace std;
class area_cl
{
public:
    double height, width;
};
class rectangle : public area_cl
{
public:
    double area(double h, double w)
    {
        return h * w;
    }
};
class cylinder : public area_cl
{
private:
    double pi = 3.14;
public:
    double area(double h, double w)
    {
        return 2 * pi * (w / 2) * ((w / 2) + h);
    }
};
int main()
{
    area_cl dimension;
    rectangle obj1;
    cylinder obj2;
    bool flag = true;
    while (flag)
    {
        cout << "What is the dimensions? (Height and Width)" << endl;
        cin >> dimension.height >> dimension.width;
        if (isdigit(dimension.height) && isdigit(dimension.width))
        {
            flag = false;
        }
        else
        {
            cout << "You are not entered number,please try again." << endl;
        }
    }
    cout << "Rectangle's area is : " << obj1.area(dimension.height, dimension.width) << endl;
    cout << "Cylinder's area is : " << obj2.area(dimension.height, dimension.width) << endl;
    return 0;
}
- I thought of using - isdigit()but my input variables must be double type and probably my code crashes for that. Are there any method in C++, like parsing in C#?
- I also thought about controlling input with using ASCII codes. For example - if ( char variable >= 48 && char variable <= 57)but I couldn't make it work.
I would prefer to solve this problem with first option but I am totally opened to other solutions.
Thanks.
 
     
    