-1

trying to get a function to work however I'm still pretty beginner at coding - this function is clearing all my inputs as valid however, as I understand it, it should be failing them?

char input;
bool valid;

do
{
    valid = false;
    gotoXY(col, row);
    cin >> input;

    if (cin.fail())
    {
        message("Invalid input! Please re-enter.", 5, offset);
        cin.clear();
        clearLine(col, row);
        clearLine(0, row + 2);
    }
    else
    {
        valid = true;

    }

    cin.ignore(150, '\n');

    clearLine2(5, offset);
    clearLine2(5, offset + 2);

} while (!valid);

cin.setf(ios::skipws);

return input;

I simply can't find a solution for this and it's really causing some headaches, any help would be appreciated!

j. m
  • 23
  • 6

2 Answers2

0

I think you wanted to the cin to fail in case you entered "1 or a string"

but the failure cases for cin could be the entered value doesn't fit in the variable.

Reference, http://www.cplusplus.com/forum/beginner/2957/ Meaning of cin.fail() in C++?

Community
  • 1
  • 1
Randolf
  • 125
  • 1
  • 9
0

As far as I can see Your code says: If Your input will fail (nothing goes to input) then send msg. else accept everything. For me it looks like You have to swap them.