Integer validation for input
When the reading fails, you set valid
to false
, so the condition in the while
loop is false
and the program returns input
(which is not initialized, by the way).
You also have to empty the buffer before using it again, something like:
#include <iostream>
#include <limits>
using namespace std;
double read_input()
{
double input = -1;
bool valid= false;
do
{
cout << "Enter a number: " << flush;
cin >> input;
if (cin.good())
{
//everything went well, we'll get out of the loop and return the value
valid = true;
}
else
{
//something went wrong, we reset the buffer's state to good
cin.clear();
//and empty it
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid input; please re-enter." << endl;
}
} while (!valid);
return (input);
}
How do i check the input is an integer in java
You use an while loop and break on sucess
int age;
while (true) { // wil break on sucess
Scanner input = new Scanner(System.in);
System.out.print("\nEnter Age : ");
String AGE = input.nextLine();
try{
age = Integer.parseInt(AGE);
break;
}catch (NumberFormatException ex){
System.out.print("Invalid input " + AGE + " is not a number");
}
}
Validation of integer with character input in C program
Some notes:
1) You aren't checking the scanf()
return value, and this is very usable: the negative return means that the entered characters can't be converted to int
(because of "%d" format), and the return value equal 0 means that the input is empty (no characters entered).
2) In case that the user entered wrong character(s) (not digit(s)), the input buffer will remain busy until you read it in other way. Good idea is to use additional scanf("%s")
here to read any characters as string, so buffer will be empty after this call. Using rewind()
is not enough here.
3) There is no need to additional checking of input in selectionChecking()
for isdigit()
, because "%d" format in scanf()
doesn't allow to read anything else but number.
4) There is no need to pass pointer to selection
value in selectionChecking()
call - it will be enough to pass it as value.
So, try this below:
// declaration of 'selectionCheck()'
int selectionCheck(int input, int min, int max);
void main()
{
int selection;
while (1)
{
while (1)
{
printf("Enter Your Selection (0-4) > ");
int ret = scanf("%d", &selection);
if (ret < 0) // invalid characters on input
{
printf("Invalid characters\n");
scanf("%s"); // empty buffer, reading it as string and putting readed characters to nowhere ;)
continue; // go to top of loop
}
if (ret == 0) // empty input
{
printf("No (empty) input\n");
continue; // go to top of loop
}
// here 'ret' is greather than 0, so valid number was entered
if (selectionCheck(selection, 0, 4)) // is value between 0 and 4 ?
break; // yes, success, break current loop!
printf("Invalid value\n");
}
printf("Success\n");
}
system("pause");
}
int selectionCheck(int input, int min, int max)
{
if (input < min || input > max)
return 0;
else
return 1;
}
Of course, you can write 'selectionCheck()' more condensed:
int selectionCheck(int input, int min, int max)
{
return (input < min || input > max) ? 0 : 1;
}
or simply:
int selectionCheck(int input, int min, int max)
{
return (input >= min && input <= max);
}
JavaScript validating input only contains integers on submit
To check if a number is an integer try this. It returns a boolean.
Number.isInteger(yourNumber)
docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests
Related Topics
How to Validate an Integer Input
How to Store Extremely Large Numbers
Properly Print Utf8 Characters in Windows Console
How to Know If a Pointer Points to the Heap or the Stack
How to Use Setprecision in C++
Detect 32-Bit or 64-Bit of Windows
Createfile: Direct Write Operation to Raw Disk "Access Is Denied" - Vista, Win7
How to Use Visual Studio Code to Compile Multi-Cpp File
Explicit Specialization in Non-Namespace Scope Does Not Compile in Gcc
C++ Return Value Without Return Statement
Detecting the Parameter Types in a Spirit Semantic Action
Are Multiple Mutations Within Initializer Lists Undefined Behavior
Inherit Interfaces Which Share a Method Name
Confusion Between C++ and Opengl Matrix Order (Row-Major VS Column-Major)