Your spec is rather incomplete.
The scanf() function has a conversion specifier for integers.
Else, you can go char by char for isdigit() and multiply by 10 each time.
Don't forget to allocate memory for string.
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
"The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. Newsgroup http://www.dejanews.com/~c_a_p
C.A.P. FAQ: ftp://38.168.214.175/pub/Chess%20Analysis%20Project%20FAQ.htm
char * string;
int integer1;
// input string for numeric values only using scanf....
..........
// set integer1 somehow to the value of string..
Many thanks.
Dann Corbit wrote:
> Allan Ashton <allana...@mywitsend99.freeserve.co.uk> wrote in message
> news:381e300c...@news.freeserve.co.uk...
> Your spec is rather incomplete.
> The scanf() function has a conversion specifier for integers.
> Else, you can go char by char for isdigit() and multiply by 10 each time.
> Don't forget to allocate memory for string.
Yes, the spec is not clear, but I belive he wants to input a string of
numbers and convert to a type int without using an standard C functions to do
the conversion.
Here is a function, asctoint() that will do this. However, it is dangerous to
use on user input because the string may not convert to a valid int. You have
the hazard of int underflow or overflow. The string may not be a valid
number. etc.
#include <stdio.h>
int asctoint(const char *string) {
const char *s1,*s2,*num = "0123456789";
int sum = 0,negative = 0;
if(*string == '-'){
negative++;
string++;
}
for(s1 = string;*s1 != '\0';s1++) {
for(s2 = num;*s2 != '\0' ; s2++)
if(*s2 == *s1) break;
if(*s2 == '\0') return sum;
sum *= 10;
sum += s2 - num;
}
return negative?sum*-1:sum;
}
int main(void) {
char *number = "-1234";
printf("The string \"%s\" convert to int is %d\n",
number,asctoint(number));
return 0;
}
--
Al Bowers
Tampa, FL USA
abo...@combase.com
http://www.gate.net/~abowers/
> #include <stdio.h>
> int asctoint(const char *string) {
> const char *s1,*s2,*num = "0123456789";
> int sum = 0,negative = 0;
> if(*string == '-'){
> negative++;
This is stylistic, of course, but I personally don't like to set a
flag with an arithmetic operation. I prefer:
#define POSITIVE 1
#define NEGATIVE 2
int sign = POSITIVE;
sign = NEGATIVE;
> string++;
> }
> for(s1 = string;*s1 != '\0';s1++) {
> for(s2 = num;*s2 != '\0' ; s2++)
> if(*s2 == *s1) break;
> if(*s2 == '\0') return sum;
There's a bug here. If I entered "-1a", the function will return 1,
not -1.
> sum *= 10;
> sum += s2 - num;
> }
Why not just:
for (s1 = string; *s1; s1++)
if (*s1 >= '0' && *s1 <= '9')
{
sum *= 10;
sum += *s1 - '0';
}
else
{
return (sign == NEGATIVE) ? -sum : sum;
}
> return negative?sum*-1:sum;
Or simpler yet,
... ? -sum : sum;
Might even be faster on truly braindead compilers.
> }
[...]
There is a bug here too :-). For strings such as "-1", "-12345" or
"12345",
the else clause will never be reached and the for loop ends when *s1 ==
'\0';
So pull the return statement out of the for loop.
--
Kien
Nice try, but no dice. :)
> For strings such as "-1", "-12345" or
> "12345", the else clause will never be reached and the for loop ends
> when *s1 == '\0';
> So pull the return statement out of the for loop.
You should've read further down, where I wrote:
> return negative?sum*-1:sum;
Or simpler yet,
... ? -sum : sum;
The return wasn't missing... from my mind, at least. ;)