I am working to add some simple user input checking into my program; there are three (3) int choices from a menu-- 1, 2, or 3. The user must choose one of these. I am able to check whether or not the input is 1, 2, or 3-- but I am not sure how to go about checking that the input is not possibly (accidently) a float or char. Any other input besides an int causes a failure and an infinite loop. I must prevent this.

I considered the idea of converting all input to int (from float or char) but I have had little sucess with my research with this so far, converting an input value to int from any possible other type (float, char, double, etc.)

Any advice, direction, or resources on this matter would be well appreciated. Thank-you in advance.

sharky_machine

Recommended Answers

All 8 Replies

C or C++?

C or C++?

WolfPack:

Sorry about that. I am writing in C++.

Thanks.

sharky_machine

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
long convert_to_int(const char* input )
{
    char* end = 0;
    long choice;
    choice = strtol( input, &end, 10 ) ;
    if ( *end != NULL )
    {
        cout << "couldn't convert " << input << " to int\n" ;
        return 0;
    }
    else
    {
        cout << "Number " << choice << "\n" ;
        return choice;
    }
}
int main () 
{
    string input = "10";
    convert_to_int(input.c_str());
    input = "10.000";
    convert_to_int(input.c_str());
    input = "a";
    convert_to_int(input.c_str());
    return 0;
}

I considered the idea of converting all input to int (from float or char) but I have had little sucess with my research with this so far, converting an input value to int from any possible other type (float, char, double, etc.)

Any advice, direction, or resources on this matter would be well appreciated. Thank-you in advance.

Are you sure you're ready for this? It's somewhat complicated ;)

First of all, your input must be in char* or string. Then you must check each character entered to see if it's the correct type of value -- like all digits for int. If there's a bad character, decide what you need to do -- convert what you can or call it an error and try again.

If all is well, convert the string and return the proper value.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
long convert_to_int(const char* input )
{
    char* end = 0;
    long choice;
    choice = strtol( input, &end, 10 ) ;
    if ( *end != NULL )
    {
        cout << "couldn't convert " << input << " to int\n" ;
        return 0;
    }
    else
    {
        cout << "Number " << choice << "\n" ;
        return choice;
    }
}
int main () 
{
    string input = "10";
    convert_to_int(input.c_str());
    input = "10.000";
    convert_to_int(input.c_str());
    input = "a";
    convert_to_int(input.c_str());
    return 0;
}

WolfPack:
when you get the chance, could you please explain this above code to me a bit in your own words?

Thank-you in advance.

sharky_machine

Are you sure you're ready for this? It's somewhat complicated ;)

First of all, your input must be in char* or string. Then you must check each character entered to see if it's the correct type of value -- like all digits for int. If there's a bad character, decide what you need to do -- convert what you can or call it an error and try again.

If all is well, convert the string and return the proper value.

WaltP:
Thank-you for your reply and help. :)

Yes, I'm sure I'm ready to use this, I just need to understand it a bit better before I make use of it.

Thanks again,
sharky_machine ;)

WolfPack:
, could you please explain this above code to me a bit in your own words?

the heart of the function convert_to_int that converts the string to int is the strtol function.
From the documentation you see that it gives you the long value of a string. If there was a character that couldn't be converted, e.g. an alphabetic char, the value of end will be that character. that is the meaning of

end is set to point to whatever is left in start after the long.

. So once the convertion is finished, you check the value of end , and if it is not null, that means some character that was not conversible is in the chracter string. If that happens return an error and exit. You can even throw some kind of exception because this is C++. input.c_str() is how to get a non-modifiable pointer to the characters contained in it.

Hope this explaination suffice.

WolfPack:

Thank-you for you reply and explanation. It makes perfect sense to me now.

sharky_machine

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.