Dann Corbit wrote: > Allan Ashton <allanasht...@mywitsend99.freeserve.co.uk> wrote in message > news:381e300c.14153455@news.freeserve.co.uk... > > Would anyone please tell me how I can do this. i.e.
> > char * string; > > int integer1;
> > // input string for numeric values only using scanf.... > > ..........
> > // set integer1 somehow to the value of string..
> 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;
> 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++;
This is stylistic, of course, but I personally don't like to set a flag with an arithmetic operation. I prefer:
> Al Bowers (abow...@gate.net) wrote: > [...] > > 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++;
> This is stylistic, of course, but I personally don't like to set a > flag with an arithmetic operation. I prefer:
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.
> 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. ;)
>Kien Ha (Kien...@Mitel.COM) wrote: >[...] >> There is a bug here too :-).
>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. ;)