943,703 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1214
  • C RSS
Aug 25th, 2008
0

Need help to convert a string to an integer

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
c_ytsui is offline Offline
1 posts
since Aug 2008
Aug 25th, 2008
-1

Re: Need help to convert a string to an integer

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 25th, 2008
1

Re: Need help to convert a string to an integer

>I use the atoi function to do the conversion
Take a look at strtol(), it would be better, atoi() has poor error handling
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 26th, 2008
0

Re: Need help to convert a string to an integer

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Aug 26th, 2008
0

Re: Need help to convert a string to an integer

bah.

extra credit if you fix my bug

Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Aug 26th, 2008
0

Re: Need help to convert a string to an integer

Click to Expand / Collapse  Quote originally posted by jephthah ...
[...]strtok... very powerful function[...]
Almost as powerful as exit()

Click to Expand / Collapse  Quote originally posted by jephthah ...
bah.

extra credit if you fix my bug

For which one?
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 26th, 2008
0

Re: Need help to convert a string to an integer

meanie




lol
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Making War card game
Next Thread in C Forum Timeline: C Beginner: Adding Two Polynomials of degree n





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC