Integer input 
Author Message
 Integer input


Quote:
>Try to convert "12 346 6789" into 3 int's using ato* and you'll see why I
>prefer strto*.

get_int(int n, char *s, int v[])   /* n limits the number of values */
{
  char *p = s;
  int cnt = 0;

  while(*p && cnt<n)
  {
    while (*p && !isdigit(*p) && *p != '-') p++;
    if(*p) v[cnt++] = atoi(p);
    if(*p == '-') p++;
    while(isdigit(*p)) p++;
  }
  return cnt;

Quote:
}

A similar routine using strtol() is shorter:

int cnvt(int n, char *s, long v[])
{
  char *p = s;
  int cnt = 0;

  while(*p && cnt<n)
   v[cnt++] = strtol(p, &p, 10);
  return cnt;

Quote:
}

but takes three times as long to run (on my machine). Probably there is
a more efficient way to use strtol().

The routine get_int() will also convert strings like "12;346;6789" or
"month 12, year 1994, gross $3452 (total)", which cnvt() won't handle.




Mon, 26 May 1997 10:40:25 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Checking Integer Input

2. Integer input

3. Integer Input

4. Test if input is integer within range

5. total newbie: how to test user input to see if its an float or integer

6. Help : How to ensure the input is Integer?

7. Input integer or character

8. input validation of integers and characters at the same time in c

9. Input Problem - integers

10. CEdit control--limiting input to integers

11. Checking an integer in a string against the max integer limit in limits.h

12. Casting a 32-bit unsigned integer to a 16-bit unsigned integer

 

 
Powered by phpBB® Forum Software