hi it may sound very easy I need a hint first of all I know how to break a string into integers but what I got is string like this "2001 , 2002 , 2003 ,2005,2006-2015" a I need this string break into separate years and if there is a dash `-` between years I need to store all years from for example 2006 to 2015.
Its one fuction from a bigger program and I dont know how to do this If u can point me in right direction
You might try something like this as a first stage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
string data = "2001 , 2002 , 2003 ,2005,2006-2015";
istringstream ss(data);
string word;
constchar comma = ',';
while (getline(ss, word, comma))
{
cout << word << endl;
}
}
Output:
2001
2002
2003
2005
2006-2015
You might want to first tidy up the string to remove any spaces - I took this data directly from the original question.
Also the <cstring> library has the strtok function with similar but different parsing capability, for example you could split the string at either a comma or a hyphen.
When you have the integer neatly in a string, the atoi() function can turn it into a numeric value if required.