atoi ( ) to return error if fail...... 
Author Message
 atoi ( ) to return error if fail......

Dear All,

My code :-

************
fgets ( a, 50, stdin);
b=atoi (a);
printf("%d", b);
*************

1st run:-

12 (enter)
12 (displayed)

2nd run:-

12abc (enter)
12 (displayed)

Why?  The truth is I entered 12ab, not 12.  I want it to display error
message when it cannot convert the whole array into integer.  Any
suggestion?

Tks.    



Thu, 01 Apr 2004 21:49:05 GMT  
 atoi ( ) to return error if fail......

Quote:
> Dear All,
> My code :-
> ************
> fgets ( a, 50, stdin);
> b=atoi (a);
> printf("%d", b);
> *************
> 1st run:-
> 12 (enter)
> 12 (displayed)
> 2nd run:-
> 12abc (enter)
> 12 (displayed)
> Why?  The truth is I entered 12ab, not 12.  I want it to display error
> message when it cannot convert the whole array into integer.  Any
> suggestion?

What you ask for can't be done with the atoi() function. The atoi()
function has no error conditions, it just chops off the erroneous
portion of the string. Look up strtol() instead.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"To doo bee doo bee doo."
   - Frank Sinatra



Thu, 01 Apr 2004 21:55:03 GMT  
 atoi ( ) to return error if fail......

Quote:

> Dear All,

> My code :-

> ************
> fgets ( a, 50, stdin);
> b=atoi (a);
> printf("%d", b);
> *************

> 1st run:-

> 12 (enter)
> 12 (displayed)

> 2nd run:-

> 12abc (enter)
> 12 (displayed)

> Why?  The truth is I entered 12ab, not 12.  I want it to display error
> message when it cannot convert the whole array into integer.  Any
> suggestion?

No error has occured. The purpose of atoi() is to
convert the initial portion of the string which
has been done.

If you want to know about characters left over (unused)
in the string have a look at the function strtol().
(I always use this in preference to atoi()).

Malcolm Kay



Thu, 01 Apr 2004 22:10:14 GMT  
 atoi ( ) to return error if fail......

[...]

Quote:
> b=atoi (a);
[...]
> 12abc (enter)
> 12 (displayed)

> Why?  The truth is I entered 12ab, not 12.  I want it to display error
> message when it cannot convert the whole array into integer.  Any
> suggestion?

Check out strtol() and strtoul()


Fri, 02 Apr 2004 00:13:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. atoi return value

2. 'atoi' return value - help

3. error checking for atoi, etc.

4. Finding errors in atoi processing

5. Returning error values vs. not returning

6. Compiler Error: tool returned an error

7. Bug: Returning, assigning into array and testing against 0 fails

8. multiple returns, failed input, and gcc

9. DllRegisterServer abc.dll failed return code :0x80070716

10. Fire_XXXEvent failed and return hr = -858993460

11. Failing to update records; CanUpdate() returns non-zero

12. GIT fails returning registered interface pointer

 

 
Powered by phpBB® Forum Software