Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

atoi or what?

Posted on 1998-08-06
4
Medium Priority
?
492 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.
Suggested Courses

610 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