Solved

atoi or what?

Posted on 1998-08-06
4
443 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 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