trying to do input checking for an alpha character inputted where a numeric one is needed. I've implemented this function firstChoice() to allow a certain response is '2' or '3' was picked and then another certain response for anything other than '0 '1' '2' or '3'. I must add another check to see if the number is an alpha character. From what I've read, both isdigit() and isalpha() use 'char' types, but for my sitation i need 'choice' as an integer type...any suggestions?:?:?:

int firstChoice()
{
    int choice;
    cin >> choice;

    if (choice==1 || choice==0)
    {
        return choice;
    }
        while(choice!=0 && choice !=1)
        {
            if (choice == 2 || choice == 3)
            {
                cout << "Enter the polynomial first\n0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n\nEnter your choice : ";
                cin >> choice;
            }
            if (choice != 0 && choice != 1 && choice != 2 && choice != 3)
            {
                cout << "'" << choice << "'is an invalid choice\nEnter your choice : ";
                cin >> choice;
            }
        }

    if (choice==1 || choice==0)
    {
        return choice;
    }
}

SAMPLE OUTPUT (what i want is in red, other is already fitted into code):

Enter your choice : 2
'2' is an invalid choice

0. (option 0)
1. (option 1)
2. (option 2)
3. (option 3)
Enter your choice : 9
'9' is an invalid choice
Enter your choice : w
'w' is an invalid choice
Enter your choice :

Do you actually need the numbers for calculation within your method? If not just take them in as char and check versus '1' (etc.) in lines 10-17.
If you really need the numbers themselves, take them in as char and convert to the numerical value by subtracting '0'

e.g.,

char one = '1';
int ione = one - '0';  //a char is really a 1 byte int anyway

Check an ASCII table to see why this works.

//a char is really a 1 byte int anyway

Check an ASCII table to see why this works.

Ha yes I was looking up variables a moment ago and realized this as well. No these numbers indicate which path to take (it is a menu, I can provide the whole thing if need be but i feel it may be complicating moreso). Technically I should be able to place a if(isdigit(choice)) {} loop at the top of my code, then at the bottom before the end of the prototype I can finish with an else {"Wrong value"}, right?

Yes, that would work. Eventually you might want to put that if else sequence into a while (or do/while) loop so that you can get the user to re-enter.

It's not not recognizing numbers that I input, for example,

Enter your choice : 1
'1' is an invalid choice
Enter your choice : 2
'2' is an invalid choice
Enter your choice :
int firstChoice()
{
    char choice;
    cin >> choice;

    if(isdigit(choice))
    {
        if (choice==1 || choice==0)
        {
            return choice;
        }
        while(choice!=0 && choice !=1)
        {
            if (choice == 2 || choice == 3)
            {
                cout << "Enter the polynomial first\n0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n\nEnter your choice : ";
                cin >> choice;
            }
            if (choice != 0 && choice != 1 && choice != 2 && choice != 3)
            {
                cout << "'" << choice << "'is an invalid choice\nEnter your choice : ";
                cin >> choice;
            }
        }

    if (choice==1 || choice==0)
    {
        return choice;
    }
    }
    else
    {
        cout << "'" << choice << "'is an invalid choice\nEnter your choice : ";
        cin >> choice;
    }
}

You need to check versus '1' etc. (with the single quotation marks). Ignore any of my previous edits.

I was just thinking about that. You need isalnum() (or probably better isdigit() )instead.

isalnum() would return true for both,
isdigit() is what I am currently using, and it doesn't identify numbers (may be my implementation though)

Yes, sorry, I missed the real problem. See my edit above. (choice == '1' etc.)

Haha getting messed up on edits, I did an edit as well:
isdigit() is what I am currently using, and it doesn't identify numbers (may be my implementation though)

Since your numbers are characters all must be enclosed in single quotes

int firstChoice()
{
    char choice;
    cin >> choice;

    if(isdigit(choice))
    {
        if (choice=='1' || choice=='0')
        {
            return choice;
        }
        while(choice!='0' && choice !='1')

is what I meant. Goes for anytime you are comparing characters.

Ahh, I see. Thank you for the help! Too tired to get anything really done tonight but this should solve my final problem. Another challenge for the morning! :S

Good luck with it. There were a couple of false starts there on my part but it should be fine now. Post back if you have probs.

Had a few more things to fix as far as the char types. Had to change my prototype to issue a char value as well as my switch statements to '1' etc. to fit with it (confused the hell out of me for the longest time). I'll post a thread in a few days of the code after the due date is over (first 150+ line of code, although about at least 50 lines are redundant ha). Nonetheless, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.