Hello Everyone,

I have been thinking about an assignment for couple of days and tried some methods but none works really perfect; I certainly don't need code, I just need some problem solving assistance. The question is, I'm supposed to read a text from a file, and then put them into 2 linked list. One(Outer) keeps the paragraphs, one(Inner) keeps the strings. Up until here it's fine, but the problem is I don't know how to read the text to recognize the paragraphs. In other words, if I use, say, "fin >> temp" then I read the words but miss the '\n' character. I need to know about the words because I need to print my text in 80 columns. In other words, I used fin >> temp to say

if ( (myLine + temp).length() > 79 )
// Then make a new node and copy the word into thew new node

then I send the word in temp to next line. So I need to know both about my words as well as my end of line. I know it's probably hard to understand but to put it simple, how could I use fin >> temp to keep the integrity of the words and use getline(fin, temp, '\n') to know about my paragraphs.

Thank you very much.

Use std::getline to read line by line.
Use a std::istringstream to extract the words in the line.

while( std::getline( file, line ) ) // read line by line
{
    if( line == "" ) // just a '\n' in file
    {
        // an empty line, new paragrah logic
    }
    else // split the line into words
    {
        std::istringstream stm(line) ;
        std::string word ;
        int chars_in_line = 0 ;
        enum { MAX_LINE_LENGTH = 80 } ;
        while( stm >> word ) // read word by word
        {
            if( ( chars_in_line + word.size() ) < MAX_LINE_LENGTH )
            {
                // print the word, and then a space
                // add word.size() + 1 to chars_in_line
            }
            else if( ( chars_in_line + word.size() ) == MAX_LINE_LENGTH )
            {
                // the word just fits on the line
                // print the word, and then a newline
                // reset chars_in_line to zero
            }
            else
            {
                // the word doesn't fit on the current line
                // print a newline, then the word, then a space
                // reset chars_in_line to word.size() + 1
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.