Hi all,

I am new to C programming and need some of your help. I am trying to convert a string(ex. 25,000) to an integer. I use the atoi function to do the conversion, however, it only reads the initial portion of the string by stopping at the first non-numerical character. In my case, it stores the value of "25" rather than "25000". How do I remove the comma and convert it to an integer? Please advise, thanks in advance.

Carol

int result;

extract_value(char* param_name)
{
	char param[256], paramvalue[2048], *valueptr, valuestr[64];
	int result;

	sprintf(param, "{%s}", param_name);	
	strcpy(paramvalue, lr_eval_string(param));

	if((valueptr = (char*)strstr(paramvalue, ">")) != NULL)
		{
		strcpy(valuestr, &valueptr[1]);
		result = atoi(valuestr);
		}
	lr_output_message("Param: %s, Value: %d", param_name, result);

	return result;
}

you can remove the comma by shifting all other characters left one place, assuming the string is in read/write memory.

char str[] = "25,000";
// locate the comma
char* ptr = strchr(str,',');
// shift remaining characters left 1 place
memmove(ptr, ptr+1, strlen(ptr+1) + 1);
commented: but mine is a bazillion lines longer than yours!! :P +4

>I use the atoi function to do the conversion
Take a look at strtol(), it would be better, atoi() has poor error handling

commented: i agree +4

check this out. compile it and run it and see how it works.

the function "stripCharacters" will strip out any undesired characters using the standard library function strtok... very powerful function, learn to use it.

#include <stdio.h>
#include <string.h>
#define MAXLEN    32

//////////////////////////////////////////////////////
//
// function : stripCharacters
// 
// input  : character string to be modified
// output : the modified string (original is lost!!)
// return : number of chars stripped, or -1 if error
//
//////////////////////////////////////////////////////
int stripCharacters(char * user_str)
{
   char copy_str[MAXLEN], *ptr;
   const char CHARS_TO_STRIP[10] = ",$%#";
   
   //error check, make sure string is not too long
   int stringlen = strlen(user_str);
   if (stringlen > MAXLEN)
       return -1;

   //make copy of user string, modified version will replace it
   strncpy (copy_str,user_str,MAXLEN);
   
   //look for first instance of "strip characters"
   ptr = strtok(copy_str, CHARS_TO_STRIP);
   strcpy(user_str,ptr);

   //look for additional strip characters
   while(ptr)
   {
      ptr = strtok(NULL, CHARS_TO_STRIP);
      if (ptr)
         strcat(user_str,ptr);
   }
   
   //modified string is passed back in original string variable
   //return value indicates number of chars stripped, if any
   return (stringlen - strlen(user_str));
}



int main()
{
   char myString[32];
   int retVal;

   strcpy(myString,"1,234,567,890,123");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"$5,299.99");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"0.039%");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"#103");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"999888777");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"123,456,789,012,345,678,901,234,567,890");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);


   return 0;
}

.

bah.

extra credit if you fix my bug

:P

[...]strtok... very powerful function[...]

Almost as powerful as exit() ;)

bah.

extra credit if you fix my bug

:P

For which one?

meanie :(


lol :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.