Re: array question
On Fri, 28 Apr 2006 13:21:32 -0700, foker wrote:
How can we diagnose code that you keep hidden from us?
I have this
string get_sentence;
This is a single string, not an array. Do you mean:
string get_sentence[15]
It's best to copy-and-paste (a short section of) your code into posts, so
that people can identify exactly what the problem is.
for(int i = 0; i < 15; i++)
Are you sure you'll always have exactly 15 sentences? It's better not to
hardcode this in - instead, use a vector which can resize to fit as many
sentences as you need.
{
fin >> get_sentence[i];
With the code you have here, that will input single character (and
produce undefined behavior, because you are trying to write to a
zero-length string). If you have an array of strings, it will (as you
report) input a string up to the first whitespace character. To read a
whole line into a string, use the getline function.
cout << get_sentence[i];
}
Try:
std::vector<std::string> sentences;
std::string one_sentence;
while( std::getline(fin, one_sentence) )
{
sentences.push_back(one_sentence);
std::cout << sentences.back() << std::endl;
}