| | |
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:
Solved Threads: 0
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:
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:
C Syntax (Toggle Plain Text)
#include <sstream> #include <string> #include <iostream> using namespace std; bool StringToInt(const string &s, int &i); int main(void) { string s1 = "12"; string s2 = "ZZZ"; int result; if (StringToInt(s1, result)) { cout << "The string value is " << s1 << " and the int value is " << result << endl; } else { cout << "Number conversion failed" <<endl; } if (StringToInt(s2, result)) { cout << "The string value is " << s2 << " and the int value is " << result << endl; } else { cout << "Number conversion failed on " <<s2 <<endl; } return(0); } bool StringToInt(const string &s, int &i) { istringstream myStream(s); if (myStream>>i) return true; else return false; }
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.
C Syntax (Toggle Plain Text)
std::string str = "48098, 50933, 3128, 323, 42, 5"; int n; char* ptr = strtok((char*)str.c_str(),","); while(ptr != 0) { n = atoi(ptr); // you will probably want to put the ints into // some sort of array or vector ptr = strtok(0,","); }
•
•
Join Date: Jan 2006
Posts: 10
Reputation:
Solved Threads: 0
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:
Thanks again.
Here's the code:
C Syntax (Toggle Plain Text)
#include <string.h> #include <stdio.h> char string[] = "48098,50933,3128,323,42,5"; char seps[] = " ,"; char *token; void main(void) { printf( "%s\n\n Tokens:\n", string); token = strtok( string, seps ); while( token != NULL ) { printf( "%s\n", token ); token = strtok( NULL, seps ); } }
Thanks again.
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
![]() |
Similar Threads
- Find the duplicate numbers in a set of ints (C++)
- Converting Pascal to VB (Visual Basic 4 / 5 / 6)
- Conversion and searching with a string help (C++)
- Converting String to Integer help (C++)
- Set Theory (Computer Science)
Other Threads in the C Forum
- Previous Thread: strocc function
- Next Thread: (C) have 2 parse errors (help)
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






