Jump to content
  • Advertisement
Sign in to follow this  
chaosgame

Array of strings

This topic is 4937 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
The easiest way is to create a std::vector<std::string> and to push_back() each word as you read it.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

#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;
}

Share this post


Link to post
Share on other sites
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 ' '

Share this post


Link to post
Share on other sites
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)[/quote]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!