Array of strings

Started by
11 comments, last by garyfletcher 19 years, 1 month ago
In C++ using iostream cin and the string class. . . How would i go about creating an array of variable length strings from a pointer where each string is an individual word from the input. It recognises how many words there are in the input however when it displays them the first word displays correctly but all subsequent words are unending gibberish. Please help
"Are you threatening me, Master Jedi?" - Chancellor Palpatine
Advertisement
The easiest way is to create a std::vector<std::string> and to push_back() each word as you read it.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks but i ment having something like:
goodbye cruel world

Turn into:

1) goodbye

2) cruel

3) world
"Are you threatening me, Master Jedi?" - Chancellor Palpatine
Yes, I understand.

std::istream& operator>>(std::istream&, std::string&) will read one word at a time for you.

Each word can then be appended to a std::vector<std::string> as I mentioned earlier.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Sorry, Im new with C++ strings. Just making sure, i can do string[n] and it will give me the nth word.
Thanks for ur help, rate++.
"Are you threatening me, Master Jedi?" - Chancellor Palpatine
Well I was hoping I could help ya on this one. If you have been through classes you might've learned that you can make a variable like int etc. into a char... well, try using that, like I am not to great with classes but try it. Like make the variable a&b*c. I may be wrong, like I said I am not to great with classes etc.
Quote:Original post by chaosgame
Sorry, Im new with C++ strings. Just making sure, i can do string[n] and it will give me the nth word.
Thanks for ur help, rate++.


std::string str; str[n] would be the nth character. You need to first break down the input into words - you said you were using std::cin. Well, every time you do std::cin >> some_string; it does read a word off standard input and into the some_string variable.

It is that word that you place in the vector (let's call it vec). THEN, when you do vec[n] you will get the nth word.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
#include <string>using std::string;using std::getline;#include <iostream>using std::cin;using std::cout;using std::endl;#include <sstream>using std::istringstream;#include <vector>using std::vector;int main(void){	// final container of strings	vector<string> string_array;	// get string of input from cin	string temp_string;	getline(cin, temp_string);	// feed it into an istringstream, which will break the string up for us	istringstream iss(temp_string);	// while there are words left in the istringstream, add them to the string container	string token;	while(iss >> token)		string_array.push_back(token);	// repeat what was entered	for(size_t i = 0; i < string_array.size(); i++)		cout << string_array << endl;	return 0;}
im not 100% sure if this works on the string class too, but on character arrays i use normaly strtok(...,...)

you may use strtok this way to parse for each word (only thos who are separated by an SPACE ' ')

cout << strtok(string, ' ');

strtok returns an pointer to an char array which holds the part of string till the next delimiter char ' '
-------------------------------------------------------------------"Debugging is twice as hard as writing the code in the first place.Therefore, if you write the code as cleverly as possible, you are,by definition, not smart enough to debug it." - Brian W. Kernighan
Quote:Original post by Becko
im not 100% sure if this works on the string class too, but on character arrays i use normaly strtok(...,...)


No, it does not, and strtok has the downside of modifying the char array you pass it. (it tokenizes by clobbering the separators with \0)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement