How to use isalpha() function?

Hey guys, I was wondering whether you should use the int data type or char data type when using the isalpha() or isdigit()function?

For example, I'm not sure whether you should use the code:
1
2
3
4
5
6
7
8
9
10
int num;
cin>>num;
if(isdigit(num))
{
    cout<<"Thank you for entering a digit";
}
else
{
    cout<<"Please enter an alphabet.";
}


Or whether I should use the code:

1
2
3
4
5
6
7
8
9
10
char num;
cin>>num;
if(isdigit(num))
{
    cout<<"Thank you for entering a digit";
}
else
{
    cout<<"Please enter an alphabet.";
}
both isalpha() and isdigit() see int and char the same way.
you will get the same thing back from both of them from putting 65 or 'A' in them.

isalpha() will return true to both of them.
isdigit() will return false to both of them.

this may help http://en.wikipedia.org/wiki/ASCII.
Last edited on
Then what is the use of these functions?
isalpha() returns true to '[a-zA-Z]' and false to everything else.
isdigit() returns true to '[0-9]' and false to everything else.

the point of these are not to tell type char from type int but to tell if the character they represent is a letter or a number, which is where the acs ii tables come in.
I was making a program that would like users to choose from 7 different menus to enter, if they entered anything besides 1, 2, 3, 4,5, 6 or 7. The program would output something to call users to enter the available options only and ask them to input again. How can I do that ?

I was considering for if the users keyed in characters. Because the code that I've accomplished can only handle with floats and integers, and if any characters are typed in. The program would be in an infinite loop.
1
2
3
4
5
6
7
8
9
    char ch = ' ';

    do
    {
        cout << "please choose 1, 2, 3, 4, 5, 6 or 7" << endl;
        cin >> ch;
    } while (ch < '1' || ch > '7');

    cout << "Thank you for entering: " << ch << endl;
Last edited on
1
2
3
4
5
6
7
8
9
 char ch = ' ';

    do
    {
        cout << "please choose 1, 2, 3, 4, 5, 6 or 7" << endl;
        cin >> ch;
    } while (ch < '1' || ch > '7');

    cout << "Thank you for entering: " << ch << endl;


This would mess up my code for dealing with floats since my code for dealing with that is.

1
2
3
4
5
6
7
8
9
10
11
float num;
cin>>num;

if(num==1||num==2||num==3||num==4||num==5||num==6||num==7)
{
     //somecodehere
}
else
{
    cout<<"Please enter 1,2,3,4,5,6 or 7.";
}
This would mess up my code for dealing with floats since my code for dealing with that is.

I thought you were allowing the user to choose from one of 7 items on a menu. Why do you use type float for this - is the user allowed to choose option for example 3.68678 or -1.7 ?

To begin by stating the solution is to put the cart ahead of the horse. You need to start by stating what are the requirements, how is the program intended to behave, purely from the external point of view, that is from the user's perspective. Only after that is clear can you determine what would be a suitable data type to capture the user input.

In any case, it is a simple enough matter to convert from one type to another, if there is a real need to do so. By 'real need' I mean to satisfy some functional requirement.
Last edited on
I thought you were allowing the user to choose from one of 7 items on a menu. Why do you use type float for this - is the user allowed to choose option for example 3.68678 or -1.7 ?

I think you need to start by stating what are the requirements, how is the program intended to behave, purely from the external point of view, that is from the user's perspective. Only after that is clear can you determine what would be a suitable data type to capture the user input.

In any case, it is a simple enough matter to convert from one type to another, if there is a real need to do so.


Yes, but I was just trying to make the program bug free. What if the user keys in a float ? or a character? Even though I've told them to key in integers?
Well, the code I suggested will read just a single character. If the user keys in the integer 1, then that will be read in and considered valid. On the other hand if the user keys in the letter 'A' that will be read in and rejected as not valid. This is a reasonably robust solution - thought it could still be further refined, but at this stage I am trying to keep things simple.

As you already mentioned, when you used type float, if the user keyed in a letter, it resulted in an infinite loop, so that is a significantly less robust approach.

You might want to try out this code which I posted yesterday in answer to a different question - it accepts only integers:
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>
#include <sstream>
#include <string>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    int choice;

    while (true)
    {
        cout << "Please enter an integer: ";
        std::string input, test;        
        cin >> input;
        std::istringstream ss(input);
        bool goodint = (ss >> choice);
        bool goodstr = (ss >> test);
        if (goodint && !goodstr)
            break;
        cout << "Invalid input: " << input << endl;
    }
        
    cout << "The integer was: " << choice << endl;

    return 0;
}

Demo: http://cpp.sh/8kb

Last edited on
Topic archived. No new replies allowed.