Re: Help with atoi function for a numero program.



On May 30, 1:48 pm, Glenn Hutchings <zond...@xxxxxxxxxxxxxx> wrote:
Ram <sriram....@xxxxxxxxx> writes:
char str[100],tmp[1];

<snip>

for ( i=0; str[i]!='\0'; i++)
{

tmp[0]=str[i];
c=atoi(tmp); // I need help here
cnt=cnt+c;
}

<snip>



The question to the group is on the atoi function.
I am converting the integer to string and so that
-> i can parse the string,
-> convert it back to integer and
-> sum it up.
( For e.g. in the above code i have sub_sum as 14)
1. i am checking for the condition is true then i am converting the
integer to string using sprintf. This works fine. The string is
declared as an character array. In this program char str[100];
2. Then using for loop to check for end of line
now str[0] = 1 and later str[1] = 4
now i use atoi to convert the string to integer so that i can
perform the sum.

Can someone please explain me what i am doing wrong. atoi needs the
string and i am passing a character so i tried passing the charecter
to a tmp[] and passing that to atoi function but doesnt solve my
purpose.

Strings in C must be terminated by a zero ('\0') character to be usable
correctly in C library functions. Your tmp array doesn't have this. If
you define it as tmp[2], and add the line

tmp[1] = '\0';

after the tmp[0] assignment, it should work.

There are much better ways to do what you're trying to do in various places
in your program: check out the sscanf() library function. It scans the
contents of a string and places the results into one or more variables
directly.

Oh, and the // style of comments are C++, not standard C, although most
compilers these days will accept them anyway. The C style of comment is
like /* this */.

Glenn

Thanks glen i got it.
.



Relevant Pages

  • Re: The Philosophy of Programming?
    ... less trivial to ensure that the string length has not been altered. ... > lowers psychological complexity for most intelligent readers. ... For most intelligent programmers, the laziness principal applies. ...
    (comp.programming)
  • Re: A C++ Whishlist
    ... > people from inclusion in a standard. ... > creating their own string class. ... >>don't want an ever increasing size of exception specification on each ...
    (comp.lang.cpp)
  • Re: Hash functions (was: Maximum String size in Java?)
    ... snip ... ... Inserted with sx31hsh and ssfh_ph in 0.385 secs ... your string library has problems with them speaks for itself. ... published your SFH code with the shifted and undersized ints. ...
    (comp.programming)
  • Re: Zero terminated strings
    ... including when you do things like string concatenation etc ... Simple "tricks" are all you need to implement a decent link level ... <snip basic link layer protocol> ...
    (comp.lang.c)
  • Performance with reading large numbers of files...
    ... I have a small test application that recurses a directory and adds all the file names to a string collection. ... RecurseDirs, sDiskFiles); ... private static void RecurseDirs ...
    (microsoft.public.dotnet.framework.performance)