split string into array

I have this string with names and numbers extracted from a file and now I'm trying to figure out how to split just the names into an array without taking in the integers as well. Is there a certain way to read the string up to a certain delimited on each line to only take in the names?
like for example if my new string contains

ex:
John Doe, 23, 34, 45,
Todd Doe, 45, 45, 56, etc. ect.

the delimiter for each line being the ','

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 #include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
	
	ifstream inputFile;
	inputFile.open("data.txt"); // Open the input file

	string line;

string name1, name2, name3, name4, name5;

	// store file into string named line
	while (!inputFile.eof() && inputFile.peek() != '$') {
		getline(inputFile, line); 
	
	} 
	
	cin.get();
	return 0;
}
Last edited on
You could do it with string.erase, string.size and use a counter to count the ascii chars that are a-z and A-Z. Or since your data has spaces just look for the next comma. and see if any of the values are numbers.

psudo code below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mystring=line;
while (i < mystring.size() )
{
if (mystring[i] > ascii a) && (mystring[i] < ascii Z)
{count ++;}
else
{
// use string erase to delete all chars after the ,
}
// use string erase to update value for line

// the name is mystring
// the remainder is line, the while loop will now continue until the end of the line.

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   string name, notName;
   ifstream in( "data.txt" );
   while ( getline( in, name, ',' ) && getline( in, notName ) ) 
   {
      cout << name << '\n';
   }
}


data.txt:
John Doe, 23, 34, 45,
Todd Doe, 45, 45, 56,


Output:
John Doe
Todd Doe
thank you!
Also quick question how do I go about taking a string like so

1
2
3
4
5
6
cout << data;

1,2,3,4,5,
6,7,8,9,0,
1,2,3,4,5
6,7,8,9,0,


and put it into a 2d integer array? so it is stored like so:
1
2
3
4
5
   0 1 2 3 4 
 0  1 2 3 4 5
 1  6 7 8 9 0
 2  1 2 3 4 5
 3  6 7 8 9 0


whats a simple way to convert a string into a 2d integer array? Or at least convert it into a 1d integer array so I can then assign it into a 2d.

thanks

Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <vector>
#include <regex>

std::vector<int> extract_integers_from( const std::string& str )
{
    // an optional + or - sign followed by a sequence of one or more decimal digits
    static const std::regex int_re( "[+\\-]?\\d+" ) ;
    static const std::sregex_iterator end ;

    std::vector<int> numbers ;
    // http://en.cppreference.com/w/cpp/regex/regex_iterator
    for( std::sregex_iterator iter( str.begin(), str.end(), int_re ) ; iter != end ; ++iter )
        numbers.push_back( std::stoi( iter->str() ) ) ; // convert the matched character to int
    return numbers ;
}

std::vector< std::vector<int> > make_2d_array( const std::string& str )
{
    std::vector< std::vector<int> > array_2d ;

    std::size_t max_row_sz = 0 ;

    std::istringstream stm(str) ;
    std::string line ;
    while( std::getline( stm, line ) ) // for each line in the string
    {
        auto row = extract_integers_from(line) ;
        if( max_row_sz < row.size() ) max_row_sz = row.size() ;
        array_2d.push_back( std::move(row) ) ;
    }

    for( auto& row : array_2d ) row.resize(max_row_sz) ; // make all rows of the same size
    return array_2d ;
}

int main()
{
    const std::string str = "1,2,3,4,5,\n"
                            "6,7,8,9,0,\n"
                            "1,2,3,4,5\n"
                            "6,7,8,9,0,\n"
                            "6,-7,8\n" // added for testing
                            "\n" // added for testing
                            "begin:6a7 (8)-9end!\n" ; // added for testing

    const auto array_2d = make_2d_array(str) ;

    for( const auto& row : array_2d )
    {
        for( int v : row ) std::cout << std::showpos << v << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/3c47e53ff3dcfdcf
Topic archived. No new replies allowed.