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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2006
Posts: 10
Reputation: wigster84 is an unknown quantity at this point 
Solved Threads: 0
wigster84 wigster84 is offline Offline
Newbie Poster

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

 
0
  #1
Jan 18th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
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: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Jan 18th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 10
Reputation: wigster84 is an unknown quantity at this point 
Solved Threads: 0
wigster84 wigster84 is offline Offline
Newbie Poster

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

 
0
  #3
Jan 18th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #4
Jan 18th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC