isdigit() always passes the check

kyrpav

Hello i want to check in my program if the user instead of typing a digit if he typed something that is not a digit.

so i did this function

void ValidationController::cinError(int *variable){

    if(!isdigit(*variable)){
        cin.clear();
        cin.ignore(256, '\n');
        cout <<*variable<<endl;
        *variable=0;
        cout <<*variable<<endl;
    }
}

i call the function like this:

int more;
cin >>more;
cinError(&more);

So my problem is that everytime i give a digit it acts like i didn't.It goes inside if and makes the variable equal to zero.What am i missing here?

Sergey Kalinichenko

Leaving aside the fact that you are using isdigit incorrectly, it's too late to check for isdigit anyway, because you are reading an int. In this situation it is the stream's >> operator that looks for digits, not your code.

If you want to validate the user input, read the data into a string, and then use isdigit on its components, like this:

string numString;
getline(cin, numString);
for (int i = 0 ; i != numString.length() ; i++) {
    if (!isdigit((unsigned char)numString[i])) {
        cerr << "You entered a non-digit in a number: " << numString[i] << endl;
    }
}
// Convert your validated string to `int`

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TCP connect() always passes

From Dev

Android unit test always passes

From Dev

Laravel5 - Request validation always passes

From Dev

The if that compares strings always passes. Why?

From Dev

how to check if the number is not an integer in C without using isdigit?

From Dev

Check when an SKSpriteNode passes a certain point or line

From Dev

Testing Laravel facades with mockery always passes, even when it should fail

From Dev

CommandParameter always passes EventArgs with ElementName binding, no actual binded value

From Dev

CommandParameter always passes EventArgs with ElementName binding, no actual binded value

From Dev

htaccess rewrite always passes filename instead of actual url

From Dev

How To Always Check For Inputs

From Dev

How to check how many passes are needed to sort the elements in an ArrayList?

From Dev

How to check how many passes are needed to sort the elements in an ArrayList?

From Dev

Is there a way other than 'try...except' and '.isdigit()' to check user input in Python 2?

From Dev

Springboot health check is always down

From Dev

Check window size always (jquery)

From Dev

Equality check seems to always be true

From Dev

isdigit() in a function

From Dev

isdigit() is not working?

From Dev

Java - TestNG : Why does my Assertion always passes when written within try-catch block

From Dev

EF SqlQuery always passes parameter with null value to my stored procedure with multiple parameter requirements

From Dev

My Java regex never matches despite it always passes on online java regex checker

From Dev

Using 'in' keyword to check for 0 always returns true?

From Dev

Laravel Hash::check() always return false

From Java

Powershell Umlaute check shows always false

From Dev

jquery check has id always goes to else

From Dev

Check if a Vector contains a string always fails

From Dev

ReSharper: Null check is always false warning

From Dev

How to check that a method always returns a value

Related Related

  1. 1

    TCP connect() always passes

  2. 2

    Android unit test always passes

  3. 3

    Laravel5 - Request validation always passes

  4. 4

    The if that compares strings always passes. Why?

  5. 5

    how to check if the number is not an integer in C without using isdigit?

  6. 6

    Check when an SKSpriteNode passes a certain point or line

  7. 7

    Testing Laravel facades with mockery always passes, even when it should fail

  8. 8

    CommandParameter always passes EventArgs with ElementName binding, no actual binded value

  9. 9

    CommandParameter always passes EventArgs with ElementName binding, no actual binded value

  10. 10

    htaccess rewrite always passes filename instead of actual url

  11. 11

    How To Always Check For Inputs

  12. 12

    How to check how many passes are needed to sort the elements in an ArrayList?

  13. 13

    How to check how many passes are needed to sort the elements in an ArrayList?

  14. 14

    Is there a way other than 'try...except' and '.isdigit()' to check user input in Python 2?

  15. 15

    Springboot health check is always down

  16. 16

    Check window size always (jquery)

  17. 17

    Equality check seems to always be true

  18. 18

    isdigit() in a function

  19. 19

    isdigit() is not working?

  20. 20

    Java - TestNG : Why does my Assertion always passes when written within try-catch block

  21. 21

    EF SqlQuery always passes parameter with null value to my stored procedure with multiple parameter requirements

  22. 22

    My Java regex never matches despite it always passes on online java regex checker

  23. 23

    Using 'in' keyword to check for 0 always returns true?

  24. 24

    Laravel Hash::check() always return false

  25. 25

    Powershell Umlaute check shows always false

  26. 26

    jquery check has id always goes to else

  27. 27

    Check if a Vector contains a string always fails

  28. 28

    ReSharper: Null check is always false warning

  29. 29

    How to check that a method always returns a value

HotTag

Archive