Checking if user input is a number or character

Hey guys,

Does anyone know how to write a programme that checks if the users input is a number or is alphabet, im 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 users 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
I made an algorithm that does what op suggests (checking if input is character).

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
#include <iostream>

bool checkAlpha(char);

int
main(void)
{
	std::cout << "Char: ";
	char ch[0];
	std::cin >> ch;
	std::cout << "\n" << checkAlpha( ch[0] );
	
	return 0;
}

bool checkAlpha(char ch)
{
	static char alpha[] = {'A','B','C','D','E','F','G','H','I','J','K',
						   'L','M','N','O','P','Q','R','S','T','U','V',
						   'W','X','Y','Z','a','b','c','d','e','f','g',
						   'h','i','j','k','l','m','n','o','p','q','r',
						   's','t','u','v','w','x','y','z'};
	for (auto &letter : alpha) {
		if ( ch == letter)
			return true;
	}
	return false;
	
}


Good luck - Isak
Last edited on
what is wrong with the built in isdigit? (granted, its pretty much just if (ch >= '0' && ch <= '9') ). Iterating over a long list checking each one should be avoided if possible. For a 10 digit input the above would check like 500 comparisons!!

the above won't work if they type in symbols or other rubbish.

And you should be careful if you plan to use Unicode etc. Then it gets really 'exciting'.
Last edited on
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
#include <iostream>

int get_int( const char* prompt = "please enter an int: " )
{
    std::cout << prompt ;
    int value ;

    if( std::cin >> value ) return value ; // user entered an int, return it

    // input failure
    std::cin.clear() ; // clear the failed state of the stream
    std::cin.ignore( 1000, '\n' ) ; // throw the bad input away
    std::cout << "invalid input. try again\n" ; // inform the user
    return get_int() ; // try again
}

int main()
{
    const int a = get_int() ;
    std::cout << "ok. the value is " << a << "\n\n";

    std::cin >> std::hex ; // accept input as hexadecimal
    const int b = get_int( "enter an int (hex): " ) ;
    std::cout << "ok. the value is " << b << " (0x" << std::hex << b << ")\n\n" ;

    const int c = get_int( "enter an int (hex): " ) ;
    std::cout << "ok. the value is " << std::dec << c << " (0x" << std::hex << c << ")\n" ;
}
please enter an int: abcd
invalid input. try again
please enter an int: 1234
ok. the value is 1234

enter an int (hex): wxyz
invalid input. try again
please enter an int: abcd
ok. the value is 43981 (0xabcd)

enter an int (hex): 0xabcd
ok. the value is 43981 (0xabcd)
please enter an int: 1error
ok. the value is 1

enter an int (hex): ok. the value is 14 (0xe)

enter an int (hex): invalid input. try again
please enter an int:  



please enter an int: 1 another error
ok. the value is 1

enter an int (hex): ok. the value is 10 (0xa)

enter an int (hex): invalid input. try again
please enter an int: 


You need to clear the rest of the line whether the input is good or bad.
Last edited on
> You need to clear the rest of the line whether the input is good or bad.

That is at best a matter of opinion; typically made under the naive belief that stdin can never be redirected; that input always comes from a user sitting in front of a terminal, waiting to type in something when prompted by the program.

If that were universally true, the designers of every i/o library would have specified that after successful formatted input of one field, the remainder of that line would be discarded; things like comma separated values or space separated values would not exist.

The three sane options are:
a. If you want an entire line to be consumed as a single field, read the entire line as a string and then parse it. In that case, in the above example, '1error' won't be good input to start with.

b. If you want an entire delimited sequence of characters to be consumed as a single field, read it as a string and then parse it. In that case too, in the above example, '1error' won't be good input to start with.

c. Otherwise, read the field and do not tamper with with what remains in the input buffer after that.
In particular, a function like get_int() has no business assuming, god-like, that what follows the int that it has successfully read must necessarily be something that must also be read in as an int.
Topic archived. No new replies allowed.