Need help to convert a string to an integer

Reply

Join Date: Aug 2008
Posts: 1
Reputation: c_ytsui is an unknown quantity at this point 
Solved Threads: 0
c_ytsui c_ytsui is offline Offline
Newbie Poster

Need help to convert a string to an integer

 
0
  #1
Aug 25th, 2008
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;
  1. extract_value(char* param_name)
  2. {
  3. char param[256], paramvalue[2048], *valueptr, valuestr[64];
  4. int result;
  5.  
  6. sprintf(param, "{%s}", param_name);
  7. strcpy(paramvalue, lr_eval_string(param));
  8.  
  9. if((valueptr = (char*)strstr(paramvalue, ">")) != NULL)
  10. {
  11. strcpy(valuestr, &valueptr[1]);
  12. result = atoi(valuestr);
  13. }
  14. lr_output_message("Param: %s, Value: %d", param_name, result);
  15.  
  16. return result;
  17. }
Last edited by Ancient Dragon; Aug 25th, 2008 at 7:35 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,339
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Need help to convert a string to an integer

 
-1
  #2
Aug 25th, 2008
you can remove the comma by shifting all other characters left one place, assuming the string is in read/write memory.
  1. char str[] = "25,000";
  2. // locate the comma
  3. char* ptr = strchr(str,',');
  4. // shift remaining characters left 1 place
  5. memmove(ptr, ptr+1, strlen(ptr+1) + 1);
Last edited by Ancient Dragon; Aug 25th, 2008 at 7:41 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,029
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Need help to convert a string to an integer

 
1
  #3
Aug 25th, 2008
>I use the atoi function to do the conversion
Take a look at strtol(), it would be better, atoi() has poor error handling
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Need help to convert a string to an integer

 
0
  #4
Aug 26th, 2008
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.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAXLEN 32
  4.  
  5. //////////////////////////////////////////////////////
  6. //
  7. // function : stripCharacters
  8. //
  9. // input : character string to be modified
  10. // output : the modified string (original is lost!!)
  11. // return : number of chars stripped, or -1 if error
  12. //
  13. //////////////////////////////////////////////////////
  14. int stripCharacters(char * user_str)
  15. {
  16. char copy_str[MAXLEN], *ptr;
  17. const char CHARS_TO_STRIP[10] = ",$%#";
  18.  
  19. //error check, make sure string is not too long
  20. int stringlen = strlen(user_str);
  21. if (stringlen > MAXLEN)
  22. return -1;
  23.  
  24. //make copy of user string, modified version will replace it
  25. strncpy (copy_str,user_str,MAXLEN);
  26.  
  27. //look for first instance of "strip characters"
  28. ptr = strtok(copy_str, CHARS_TO_STRIP);
  29. strcpy(user_str,ptr);
  30.  
  31. //look for additional strip characters
  32. while(ptr)
  33. {
  34. ptr = strtok(NULL, CHARS_TO_STRIP);
  35. if (ptr)
  36. strcat(user_str,ptr);
  37. }
  38.  
  39. //modified string is passed back in original string variable
  40. //return value indicates number of chars stripped, if any
  41. return (stringlen - strlen(user_str));
  42. }
  43.  
  44.  
  45.  
  46. int main()
  47. {
  48. char myString[32];
  49. int retVal;
  50.  
  51. strcpy(myString,"1,234,567,890,123");
  52. printf("original: %s\n",myString);
  53. retVal = stripCharacters(myString);
  54. printf("modified: %s (stripped %i)\n\n",myString,retVal);
  55.  
  56. strcpy(myString,"$5,299.99");
  57. printf("original: %s\n",myString);
  58. retVal = stripCharacters(myString);
  59. printf("modified: %s (stripped %i)\n\n",myString,retVal);
  60.  
  61. strcpy(myString,"0.039%");
  62. printf("original: %s\n",myString);
  63. retVal = stripCharacters(myString);
  64. printf("modified: %s (stripped %i)\n\n",myString,retVal);
  65.  
  66. strcpy(myString,"#103");
  67. printf("original: %s\n",myString);
  68. retVal = stripCharacters(myString);
  69. printf("modified: %s (stripped %i)\n\n",myString,retVal);
  70.  
  71. strcpy(myString,"999888777");
  72. printf("original: %s\n",myString);
  73. retVal = stripCharacters(myString);
  74. printf("modified: %s (stripped %i)\n\n",myString,retVal);
  75.  
  76. strcpy(myString,"123,456,789,012,345,678,901,234,567,890");
  77. printf("original: %s\n",myString);
  78. retVal = stripCharacters(myString);
  79. printf("modified: %s (stripped %i)\n\n",myString,retVal);
  80.  
  81.  
  82. return 0;
  83. }


.
Last edited by jephthah; Aug 26th, 2008 at 7:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Need help to convert a string to an integer

 
0
  #5
Aug 26th, 2008
bah.

extra credit if you fix my bug

Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,029
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Need help to convert a string to an integer

 
0
  #6
Aug 26th, 2008
Originally Posted by jephthah View Post
[...]strtok... very powerful function[...]
Almost as powerful as exit()

Originally Posted by jephthah View Post
bah.

extra credit if you fix my bug

For which one?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Need help to convert a string to an integer

 
0
  #7
Aug 26th, 2008
meanie




lol
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC