chevron_left
226 points
13 5 3

When signing up an account for a website, an application, …, we are often asked to set a password that satisfies several criterials like: length, use of upper case letters and lower case letters, consecutive numbers and so on. In this article, I will show you how to implement a simple mechanism for checking user inputs.

For code readability, I will pass user inputs to a function to check their validity instead of checking directly:

bool inputNotValid(string input);

This function will return true if the input is not valid.

Next, we need an infinite loop and you can only exits the loop by providing a valid input:

string input;

do
{
	cin >> input;
} 
while (inputNotValid(input));

The reason for using do-while instead of while loop is that while loops check conditions before executing codes inside it. This means that validity of user input will be verified even before an input is entered. Vice versa, do-while loops check conditions after running codes inside it.

Suppose that an input is valid if it: has length of at least 10. We will define the function for checking this criteria:

bool inputNotValid(string input)
{

	if(input.length() < 10)
	{
		cout << "Input must contain at least 10 characters" << endl;
		return true;      // input not valid
	}
	else
	{
		return false;	  // input valid
	}
}

Result of running the code above:

abc
Input must contain at least 10 characters
addaef
Input must contain at least 10 characters
123
Input must contain at least 10 characters
afs
Input must contain at least 10 characters
fasf
Input must contain at least 10 characters
0@141244
Input must contain at least 10 characters
0123456789

Completed code:

#include <iostream>

using namespace std;

bool inputNotValid(string input);

int main()
{
	string input;


	// asking user to enter input until it is valid
	do
	{
		cin >> input;
	} 
	while (inputNotValid(input));
}

bool inputNotValid(string input)
{
	if(input.length() < 10)
	{
		cout << "Input must contain at least 10 characters" << endl;
		return true;      // input not valid
	}
	else
	{
		return false;	  // input valid
	}
}

 

More Posts

test if a number input from the keyboard is a valid test score (between 0 and 100 inclusive).Using for,while,do-while DivyaN - Aug 19
How to get the user input in C++? Lalit Kumar - Aug 7, 2020
prompt user for input in javascript DivyaN - Aug 20
How to ask user for input in java Hasnain_khan - Oct 12, 2020
Wap in python to take input from user as array and find the sum of array sundar - May 15
Write a program to accept a sentence from the user and print each word in a new line in c sakshi - Aug 28
How to assign user input to a variable in java offonime - May 22
How to check whether the input is a number or string in C++? Lalit Kumar - Jun 19, 2020
Given a list of non negative integers, arrange them such that they form the largest number. sanchi - May 11
Given a list of non negative integers, arrange them such that they form the largest number. Sayan - May 1