Input Validation

Hello everyone. I am trying to validate the user input but since I'm using unsigned long long, I'm unable to check against negative numbers. I only want the application to give give results if the input is positive. I also need a workaround for alphanumeric input.

From what I have so far, my program can differentiate between alphanumericals and numerical if the input starts with a character, an alphabet. But if I start with a number, it starts computing.

Here is the code :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream> 
#include <cmath> 
#include <limits>
using namespace std; 

unsigned long long n; 
unsigned long long number = 0; 
unsigned long long limit; 
    
void calculation() {  
        if (n % 2==0)
          for (; n % 2==0; n = n/2)
            cout << n << endl; 
        else if (n % 2!=0 && n!=1) 
          for (; n % 2!=0; n = (n+1))
            cout << n << endl; } 
            
void input_validation() { 
    while (!cin) {
        cin.clear(); 
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin.sync(); 
        cout << "Please enter a valid input - "; cin >> limit; cout<<endl;} }

int main(){
    cout << "Please enter a limit - "; cin >> limit; 
    input_validation(); 
    while (limit<1) { 
    cout << "Please enter a number bigger than 1 - "; cin >> limit; cout<<endl;}
    do {
        number++; 
        n = number; 
    cout << "Evaluating for - " << n << endl; 
    
    if (n>0); { 
    do {
        calculation(); 
    } while (n>1); 
    cout << 1 << endl; 
    } 
    } while (number<limit);
    return 0; 
} 
Last edited on
One way is to peek into the input stream to see if there is a leading '-' character in the pending input.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

// return true if the next non-white-space character to be read from the input buffer is '-'
bool negative_input() { return ( std::cin >> std::ws ).peek() == '-' ; }

unsigned long long get_positive_integer_greater_than( unsigned long long lbound )
{
    std::cout << "please enter an integer greater than " << lbound << ": " ;

    unsigned long long number ;

    if( negative_input() ) std::cout << "please enter a non-negative value. " ;

    else if( std::cin >> number ) // if the user entered a number
    {
        // return it if it is within the valid range
        if( number > lbound ) return number ;

        // not a valid choice; inform the user about the error
        else std::cout << "value is not greater than " << lbound << ". " ;
    }

    else // the user entered a non-numberic value eg. abcd
        std::cout << "invalid input. please enter a number. " ;

    // we have not returned from the function, ergo input failure
    std::cin.clear() ; // clear the (possible) failed state of the stream
    std::cin.ignore( 1000, '\n' ) ; // throw the bad input away

    std::cout << "try again\n" ;
    return get_positive_integer_greater_than(lbound) ; // try again
}

int main()
{
    const unsigned long long n = get_positive_integer_greater_than(99) ;
    std::cout << "\nyou entered " << n << '\n' ;
}
@JLBorges - Thank you! Although, it would be better to have predefined function in say the standard library to ignore certain inputs like characters which don't belong to a data type. It would also be better to have new data types without any corresponding belonging to another data type. I wonder if they used separate binary values for each individual character when designing programming languages or not.
Topic archived. No new replies allowed.