jeudi 14 novembre 2019

Is there a way to do a character/string restriction?

//The idea is, how to restrict the user to these specific symbols? p.s in the num_1/2 iput as: 10 is valid asd10 not valid

do
    {
        //Input
        cout << "Enter first the arithmatic operation (+, -, *, /, %) and then both operands: "; cin >> operation; cin >> num_1; cin>> num_2;
        if (cin.fail()) {
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            continue;
        }

    } while (   (operation != '+' && operation != '-' && operation != '*' && operation != '/' && operation != '%' )
            && ((num_1 <= '0' || num_1 <= '9') || (num_1 >= 'A' && num_1 <= 'F'))
            && ((num_2 >= '0' && num_2 <= '9') || (num_2 >= 'A' && num_2 <= 'F'))   );

Aucun commentaire:

Enregistrer un commentaire