NabePup
Est. Contributor
- Messages
- 637
- Role
- Adult Baby
- Diaper Lover
- Babyfur
- Diaperfur
- Little
I'm trying to write a program in C++ that separates a string into its separate elements if it contains multiple elements separated by spaces.
For instance, the string "john hubert williams doe" would be stored as a vector containing the values {"john", "hubert", "williams", "doe"} at indexes 0-3. It works for the first 3 indexes, but then I have trouble accessing any values stored beyond the 2nd index.
Here's the source:
I've added some cout statements for debugging purposes. And here's the output I'm getting...
Anything that's at the 3rd or greater index of the vector is this weird thing whenever I try accessing the data stored there. Yet I have no trouble accessing the data stored at those indexes earlier (one line 49 of the output, it accesses the string "name4" correctly).
For instance, the string "john hubert williams doe" would be stored as a vector containing the values {"john", "hubert", "williams", "doe"} at indexes 0-3. It works for the first 3 indexes, but then I have trouble accessing any values stored beyond the 2nd index.
Here's the source:
Code:
1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5
6
7 int main() {
8
9 string client_name;
10 int vector_index = 0;
11 vector<string> names[vector_index];
12
13 cout << "Input a name: ";
14 getline(cin, client_name);
15
16 cout << "Starting iteration through: " << client_name << "\n\n";
17 for (int i = 0; i < client_name.length(); i++)
18 {
19 cout << "Iteration: " << i << endl;
20 string temp_string;
21 cout << "\'" << client_name[i] << "\' is not a space or end. Iterating"
22 << " through valid chars to add to index.\n";
23 while (client_name[i] != ' ' && client_name[i] != 0 )
24 {
25 cout << "Adding \'" << client_name[i] << "\' to temp_string."
26 << endl;
27 temp_string += client_name[i];
28 i++;
29 }
30 cout << "Assigning \"" << temp_string << "\" to index " <<
31 vector_index << " of names vector.\n";
32 names[vector_index].push_back(temp_string);
33 cout << "Clearing temp_string.\n";
34 temp_string.clear();
35 cout << "New names vector at index " << vector_index << " is \"" << names[vector_index][0] << "\".\n";
36 cout << "Incrementing vector index to " << vector_index + 1 <<
37 "\n\n";
38 vector_index++;
39 }
40
41 cout << "Original input is \"" << client_name << "\"\n";
42 for (int i = 0; i < vector_index; i++)
43 {
44 cout << "Index " << i << " of names vector is \"" << names[i][0] <<
45 "\"\n";
46 }
47
48 return 0;
49 }
I've added some cout statements for debugging purposes. And here's the output I'm getting...
Code:
1 Input a name: word1 word2 word3 word4
2 Starting iteration through: word1 word2 word3 word4
3
4 Iteration: 0
5 'w' is not a space or end. Iterating through valid chars to add to index.
6 Adding 'w' to temp_string.
7 Adding 'o' to temp_string.
8 Adding 'r' to temp_string.
9 Adding 'd' to temp_string.
10 Adding '1' to temp_string.
11 Assigning "word1" to index 0 of names vector.
12 Clearing temp_string.
13 New names vector at index 0 is "word1".
14 Incrementing vector index to 1
15
16 Iteration: 6
17 'w' is not a space or end. Iterating through valid chars to add to index.
18 Adding 'w' to temp_string.
19 Adding 'o' to temp_string.
20 Adding 'r' to temp_string.
21 Adding 'd' to temp_string.
22 Adding '2' to temp_string.
23 Assigning "word2" to index 1 of names vector.
24 Clearing temp_string.
25 New names vector at index 1 is "word2".
26 Incrementing vector index to 2
27
28 Iteration: 12
29 'w' is not a space or end. Iterating through valid chars to add to index.
30 Adding 'w' to temp_string.
31 Adding 'o' to temp_string.
32 Adding 'r' to temp_string.
33 Adding 'd' to temp_string.
34 Adding '3' to temp_string.
35 Assigning "word3" to index 2 of names vector.
36 Clearing temp_string.
37 New names vector at index 2 is "word3".
38 Incrementing vector index to 3
39
40 Iteration: 18
41 'w' is not a space or end. Iterating through valid chars to add to index.
42 Adding 'w' to temp_string.
43 Adding 'o' to temp_string.
44 Adding 'r' to temp_string.
45 Adding 'd' to temp_string.
46 Adding '4' to temp_string.
47 Assigning "word4" to index 3 of names vector.
48 Clearing temp_string.
49 New names vector at index 3 is "word4".
50 Incrementing vector index to 4
51
52 Original input is "word1 word2 word3 word4"
53 Index 0 of names vector is "word1"
54 Index 1 of names vector is "word2"
55 Index 2 of names vector is "word3"
56 Index 3 of names vector is ""q???"q??pVq??"
Anything that's at the 3rd or greater index of the vector is this weird thing whenever I try accessing the data stored there. Yet I have no trouble accessing the data stored at those indexes earlier (one line 49 of the output, it accesses the string "name4" correctly).
Last edited: