Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

atoi or what?

Posted on 1998-08-06
4
447 Views
Last Modified: 2010-04-02
Ok, the problem that Im having is that Iam simplly trying to add a failsafe into my code which is already written!
The code accepts an integer input but what if the user presses a character key (accidently of course). I have tried using 'int =  atoi(char)' which works great but if I do press a character key then a zero is placed in the input! Does anybody have a suggestion that might resolve this problem?

Thanks for any help,

bbarnette
0
Comment
Question by:bbarnette
  • 2
4 Comments
 
LVL 8

Expert Comment

by:trestan
ID: 1169547
Use isalpha to check whether your input is a char.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1169548
Pls refer to the online help for the group of Character Classification routines, which tests a specified single-byte character, wide character, or multibyte character for satisfaction of a condition.
By the way, you have post a duplicate question.
0
 

Author Comment

by:bbarnette
ID: 1169549
I have tried isalpha and that did not solve it either
0
 
LVL 2

Accepted Solution

by:
mlev earned 50 total points
ID: 1169550
if (sscanf(string, "%d", &integer) != 1) {
  /* bad input */
}

But this still allows for nondigits at the end.

This doesn't:
if (strspn(string, "-0123456789") != strlen(string)) {
  /* bad input */
} else {
  integer = atoi(string);
}

Or this:
int i;
for (i = 0; isdigit(string[i]) || string[i] == '-'; i++);
if (string[i] != '\0') {
  /* bad input */
} else {
  integer = atoi(string);
}
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamically allocate memory 9 62
max float value 3 51
object oriented programming comparison 5 77
Finding Divisors 5 19
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question