C++ Input verification

Hey guys,

Does anyone know how to write a programme that checks if the user's input is a number or is alphabet, I'm new to c++ and am trying to make a programme that does that,
For e.g
A programme which u input a number and the number gets multiplied but i want the programme to know when the user's input is alphabet and prints out a message saying "Only numbers allowed"

If you guys know how to do this please show me, it would be much appreciated👍
Thanks guys
Last edited on
Take a look at this:

http://www.cplusplus.com/reference/cctype/
http://www.cplusplus.com/reference/cctype/isalnum/

By the way: You shouldn't mark the issue as solved when it isn't.
Thanks, those were really helpful, but I'm looking for numbers, just plain numbers (12, 18)
With std::cin you can actually detect whether the input is numeric. See (the example):

http://en.cppreference.com/w/cpp/io/basic_istream/ignore
coder777 - Could you write it in simple terms, please.
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
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

// Some possible checking functions
template <typename T> bool anything( T value ) { return true; }
bool isPositiveInteger( int i ) { return i > 0; }
bool isInRange( double d ) { return d >= -1 && d <= 1; }
bool isUpperCase( char c ) { return isupper( c ); }
bool isLetter( char c ) { return isalpha( c ); }

//======================================================================

template <typename T> T getVal( const char *prompt, bool (*checker)( T ) = anything )
{
    T value;
    string rest;
    bool ok = false;

    cout << prompt;

    while ( !ok )
    {
       ok = ( cin >> value && checker( value ) );                     // Check able to read desired type and in suitable range
       cin.clear();                                                   // Clear any failed state

       getline( cin, rest );                                          // Get anything else on the line (and clear the line feed)
       ok = ( ok && rest.find_first_not_of( " " ) == string::npos );  // Check there is nothing else on the line

       if ( !ok ) cout << "Invalid; please re-enter: ";               // If invalid, re-enter
    }

    return value;
}

//======================================================================

int main()
{
   int i = getVal<int>( "Please enter an integer: " );
   cout << "You entered " << i << "\n\n";

   i = getVal<int>( "Please enter a positive integer: ", isPositiveInteger );
   cout << "You entered " << i << "\n\n";

   double d = getVal<double>( "Please enter a double between -1.0 and 1.0: ", isInRange );
   cout << "You entered " << d << "\n\n";
   
   char c = getVal<char>( "Please enter a single upper-case char: ", isUpperCase );
   cout << "You entered " << c << "\n\n";

   c = getVal<char>( "Please enter a letter: ", isLetter );
   cout << "You entered " << c << "\n\n";
}



Please enter an integer: 1f
Invalid; please re-enter: 3 f
Invalid; please re-enter: 3
You entered 3

Please enter a positive integer: -4
Invalid; please re-enter: 4.0
Invalid; please re-enter: 4
You entered 4

Please enter a double between -1.0 and 1.0: 7.5
Invalid; please re-enter: 0.5
You entered 0.5

Please enter a single upper-case char: SS
Invalid; please re-enter: S S
Invalid; please re-enter: S
You entered S

Please enter a letter: &
Invalid; please re-enter: d
You entered d

Topic archived. No new replies allowed.