Hi everyone,
How do I make sure that user input is of integer data type. not char nor alphanumeric..? when the user inputs character string or alphanumerics it should output "Please enter an integer" Is there sopme wayh to do this?
Thanks.
ssmg
0
Newbie Poster
Recommended Answers
Jump to PostIf they have input only integer data then all there should be is a string of digits.
- So get you get a line of input
- Extract an integer from it
- Verify the rest of the line is blank
If either step 2 fails or the rest of …
Jump to Post
atoi()
is unlikely to help because it has no error output, you could usestrtol()
however neither of those are C++ they are part of the C standard library and C++ provides the same functionality through the use of stringstreams.
Jump to Post
atoi()
returns 0 on error.strtol()
is the same function but converts it to long int.If the text length is greater than 1 and the output is 0, that would indicate an error. Unless someone really wanted to put 000 in there or something.
Using stringstream …
Jump to Post
atoi()
returns 0 on error.strtol()
is the same function but converts it to long int.If the text length is greater than 1 and the output is 0, that would indicate an error. Unless someone really wanted to put 000 in there or something.
Using stringstream to cast …
Jump to PostI am mildly surprised that using a stringstream is less efficient
It's makes perfect sense when you consider what's involved. Underneath
iss>> value
is a metric shitload of complex interwoven parts, only one of which performs a conversion from a streamed sequence of characters to an integer. On the other …
All 21 Replies
Banfa
597
Posting Pro
Featured Poster
Ketsuekiame
860
Master Poster
Featured Poster
Banfa
597
Posting Pro
Featured Poster
ssmg
0
Newbie Poster
Ketsuekiame
860
Master Poster
Featured Poster
Banfa
597
Posting Pro
Featured Poster
Banfa
597
Posting Pro
Featured Poster
Ketsuekiame
860
Master Poster
Featured Poster
ssmg
0
Newbie Poster
Ketsuekiame
860
Master Poster
Featured Poster
Banfa
597
Posting Pro
Featured Poster
mitrmkar
1,056
Posting Virtuoso
ssmg
0
Newbie Poster
Narue
5,707
Bad Cop
Team Colleague
Banfa
597
Posting Pro
Featured Poster
Narue
5,707
Bad Cop
Team Colleague
jonsca
commented:
Can you translate a metric shitload to American units? :)
+4
hypernova
0
Newbie Poster
Ketsuekiame
860
Master Poster
Featured Poster
Banfa
597
Posting Pro
Featured Poster
Narue
5,707
Bad Cop
Team Colleague
hypernova
0
Newbie Poster
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.