943,931 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2992
  • C RSS
Jan 18th, 2006
0

Converting a set of numbers seperated by commas...HELP!!!

Expand Post »
If i have a set of numbers:
48098, 50933, 3128, 323, 42, 5..etc

how can i convert them into integers? I have an example of a code (see below) that will convert one number, not the whole set. So, im having trouble trying to convert a set of numbers seperated by commas into integers.

PLEASE HELP!!!!!!

*NOTE - im reading a .txt file from wordpad using "fstream"

Here is the example of the code:

  1. #include <sstream>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. bool StringToInt(const string &s, int &i);
  8.  
  9. int main(void)
  10. {
  11. string s1 = "12";
  12. string s2 = "ZZZ";
  13. int result;
  14.  
  15. if (StringToInt(s1, result))
  16. {
  17. cout << "The string value is " << s1
  18. << " and the int value is " << result << endl;
  19. }
  20. else
  21. {
  22. cout << "Number conversion failed" <<endl;
  23. }
  24. if (StringToInt(s2, result))
  25. {
  26. cout << "The string value is " << s2
  27. << " and the int value is " << result << endl;
  28. }
  29. else
  30. {
  31. cout << "Number conversion failed on " <<s2 <<endl;
  32. }
  33. return(0);
  34. }
  35.  
  36. bool StringToInt(const string &s, int &i)
  37. {
  38. istringstream myStream(s);
  39.  
  40. if (myStream>>i)
  41. return true;
  42. else
  43. return false;
  44. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wigster84 is offline Offline
10 posts
since Jan 2006
Jan 18th, 2006
0

Re: Converting a set of numbers seperated by commas...HELP!!!

you can use strtok() -- warning, that function changes the string, so if you need the original string later in the program you will have to make a copy of it before calling strtok() the first time.
  1. std::string str = "48098, 50933, 3128, 323, 42, 5";
  2. int n;
  3. char* ptr = strtok((char*)str.c_str(),",");
  4. while(ptr != 0)
  5. {
  6. n = atoi(ptr); // you will probably want to put the ints into
  7. // some sort of array or vector
  8. ptr = strtok(0,",");
  9. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 18th, 2006
0

Re: Converting a set of numbers seperated by commas...HELP!!!

Thanks for the code....however, i kept on getting errors for some reason...so i kind of manipulated it and came out with this code, and it worked out pretty well...what helped was knowing strtok().

Here's the code:

  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. char string[] = "48098,50933,3128,323,42,5";
  5. char seps[] = " ,";
  6. char *token;
  7.  
  8. void main(void)
  9. {
  10. printf( "%s\n\n Tokens:\n", string);
  11.  
  12. token = strtok( string, seps );
  13.  
  14. while( token != NULL )
  15. {
  16. printf( "%s\n", token );
  17.  
  18. token = strtok( NULL, seps );
  19. }
  20. }

Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wigster84 is offline Offline
10 posts
since Jan 2006
Jan 18th, 2006
0

Re: Converting a set of numbers seperated by commas...HELP!!!

If the current protocol is satisfactory for you, so be it. You might also be able to accomplish essentially the same thing by processing each number as you read it in, rather than read in all the numbers at once and parse the whole file/string. To do that, I'd use getline() with comma as the delimiting character to read a string representing a single number into a buffer and convert the buffer on the fly. This approach might work better if you don't know the number of numbers or the number of digits in the numbers you're going to need to hold the composite string and you aren't hardcoding the string but reading it on the fly.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: strocc function
Next Thread in C Forum Timeline: (C) have 2 parse errors (help)





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


Follow us on Twitter


© 2011 DaniWeb® LLC