Solved

atoi or what?

Posted on 1998-08-06
4
426 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
Comment Utility
Use isalpha to check whether your input is a char.
0
 
LVL 8

Expert Comment

by:trestan
Comment Utility
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
Comment Utility
I have tried isalpha and that did not solve it either
0
 
LVL 2

Accepted Solution

by:
mlev earned 50 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now