Solved

atoi or what?

Posted on 1998-08-06
4
462 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
[X]
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
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.

719 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