Hi,
I am writing a little program in C++ that is used to read a file which contains several paragraphs. The paragraphs are not indented and there are a blank line or sometimes two blank lines before the start of each paragraph.
I have written the program and can read and dump the file into a new file.
Aside from the basic reading of the file and closing the file, I have a while loop with a getline to read each line. I have put an If else statement into it so as I can indent the start of a new paragraph when there is one.
The problem that I am having is I do not know what to put into the "if()" statement to denote the end of blank lines so that I can indent some sppaces. I have searched, read and searched some more for some answers on my own and finally came here for a tip.

Please help me!
Thank you

Here is the While statement that I have going on so far:

while(inFile)
   {
    getline(inFile,inputStr);

    if ()
     {
       outFile << "     " << inputStr << endl;
     }
    else
     {
       outFile << inputStr << endl;
     }
   }

I would use a flag to indicate the end of a paragraph -- the first blank line is the end of the paragraph. When the flag then indent the next non-blank line and reset the flag to false.

The while loop is better written like this

while( getline(inFile,inputStr) )
{
    if ( <flag is set> )
     {
       outFile << "     " << inputStr << endl;
       <set flag to false>
     }
    else
     {
       outFile << inputStr << endl;
       <if inputStr is an empty string set flag to true >
     }
}

Thank you for the quick reply.

"first blank line is the end of the paragraph"
Between some of the paragraphs there are two blank lines so Im going to need some type of syntax to note where the begining of a new line of text is, that is something that I cannot seem to find out how to do.


"<if inputStr is an empty string set flag to true >"
Sorry, I am not that advanced and do not know how to note if a
string is an empty one or not.

Thank you again

>>Sorry, I am not that advanced and do not know how to note if a string is an empty one or not.

a string is empty when any one of these conditions exist.

inputStr == "";

or

inputStr.length() == 0

or
inputStr.size() == 0

as for multiple blank lines -- it doesn't matter how many blank lines there are, just set the flag to true each time a blank line is detected.

Don't forget to consider the following situations:

• The blank lines may be composed of only spaces. You might need to write a trim function which first trims the lines read and then processes them.

• The first line can be a non blank requiring you to initialize the flag accordingly.

The trim function can be as simple as (not my function):

#include <string> 
 
const std::string whiteSpaces( " \f\n\r\t\v" ); 
 
void trimRight( std::string& str, 
       const std::string& trimChars = whiteSpaces ) 
 { 
    std::string::size_type pos = str.find_last_not_of( trimChars ); 
    str.erase( pos + 1 ); 
 
} 
 

void trimLeft( std::string& str, 
       const std::string& trimChars = whiteSpaces ) 
 { 
    std::string::size_type pos = str.find_first_not_of( trimChars ); 
    str.erase( 0, pos ); 
 } 
 

void trim( std::string& str, const std::string& trimChars = whiteSpaces ) 
 { 
    trimRight( str, trimChars ); 
    trimLeft( str, trimChars ); 
 }

Thank you for taking the time to give me some hints here.
I have taken your tips into consideration and have formulated this here:

isSafe = true;
     while(getline(inFile,inputStr))
     {
      if(isSafe)
       {
        outFile << "     " <<inputStr <<endl;
        isSafe = false;
       }
        else if ((inputStr == "") || (inputStr.length() ==0) || (inputStr.size() ==0))
       {
        isSafe = true;
       }
     else
     {
       outFile << inputStr << endl;
     }

   }

It looks as though im really in the same cicumstance as before where as initially I set the flag to true and yes it indents the first paragraph and then the flag is set back to false and then just runs through the rest of the loop as false print out the rest of the file as it sees it.
What im still lacking here is the knowledge of how the flag knows that it can flip back to true. Knowing when to indent; Im still not finding anywhere in my book nor the net what to use a acknowledgment that the marker has "run back into words after lines are empty".
?any ideas?

Thanks again

Like I said before, trimming the string before processsing it is the way to go in your case. After trimming the string, if its length is zero (check only for length and not the other two), then flip the binary flag.

Maybe something like this: (not tested)

void leftTrim(string& inStr, const string delimiters = " \t\n\r\v\f")
{
    string::size_type pos = inStr.find_first_not_of(delimiters);
    inStr.erase(0, pos);
}

int main ()
{
    string line;
    bool flag = true;
    ifstream inFile("test.txt");
    ofstream outFile("output.txt");
    while(getline(inFile, line))
    {
        leftTrim(line);
        if(line.size() == 0)
        {
            flag = true;
            continue;   //skip current line and move to next one
        }
        if(flag)    //if a new paragraph is afoot
        {
            outFile << endl << "    " << line << endl;
            flag = false;
        }
        else        //its a normal line
        {
            outFile << line << endl;
        }
    }
    //close the streams and wait for keypress
    inFile.close();
    outFile.close();
    getchar ();
}

It seems as if that did the trick, Its funny that I was never tought that nor could find it in any books, I appreciate the knowledge.

Just curious, what does the:

getchar ();

mean at the bottem? I am not familiar with that.

Thanks again

getchar is a standard C /C++ functiona used for getting data from the standard input stream (in normal cases keyboard). It is buffered in the sense it does not see the characters unless the user presses the return key. Since we want to prevent the console window from disappearing after the program execution, we put it at the end of the program before the return statement if any is present.

That makes sence, I am suprized that I have not run into that just yet.
Thanks for the tip.

Back to the drawing board.
There must be a way of accomplishing this task by simply using a if statement with something along the lines of "if not new line read, else indent"

Here is what Im working with now and it doesnt seem to be doing much of anything but reading and spitting out the file:

string inputStr;
 while(inFile)
   {
    getline(inFile,inputStr);

    if (!'\n')
     {
       outFile << "     " << inputStr << endl;
     }
    else
     {
       outFile << inputStr << endl;
     }
   }

Any ideas?
Thank you

I don't know what you are trying to achieve by doing if(!'\n'). This condition will never be true hence the whole file is given out as it is.

Also the trouble with this approach is that even lines which are not preceded by newlines but have indentations will always look indented. So you would have no way of knowing which lines were indented by the algorithm or which lines were indented from the start.

For proper formatting, trimming is a must.

I did do alot of reading on trimming which I did not know before to educate myself and I then explained to my teacher what I have all learned about trimming when I presented the solution to him.

He then explained to me that he has not yet tought us trimming and that there is a way to solve the problem with all the he had tought us thus far (which isnt much at all).

He then gave me the hint that I needed to use the newline character to denote the starting up of a new paragraph.

I have been trying so many variations of this while/If/else statement, which seems right with the new line character to no avail.

I am still searching my book and the inet inorder to come up with a solution at the moment.

There must be a way somehow and I will bet that it is petty simple, I just have not found it yet.

Let me know if any of you have any hints that I may be able to use in solving this problem.

Have you been taught the find function in C++? If so then you can search the string for the character which doesn't belong to the family of whitespace characters. If you find one, write the line as it is, and if no character other than whitespace character is found (indicates blank line) just indent and move on to the next line.

//check for a blank line
if(find_first_not_of(" \t\r\f\n\v") == string::npos)
{
     //write a indent to the new file
}
else
{
    // write the line as it is
}

Interesting, but no we have not.

Im still plugging away to no avail YET.

Thanks for the thought.

So I guess the only option left is to manually parse the string character by character and draw your own results. Best of luck.

I am assuming you are talking about the get function.
Because I am expected to solve this using these basic resources, there must be a way of denoting a blank line that I have yet to find.

Thank you for your help, I appreciate it.

Another hint is to use a "state machine" to handle this problem.

The problem I am running into is finding the logic behind noting when there is a new line that contains characters as opposed to blank lines.

Please help!

Like I said before, you need to read in the entire line in a string and start parsing it yourself, treating the string as an array of characters. For this, you will need functions which help you in determining whether a character is a space character or not. The function I am talking about is isspace().

for(int i = 0; i < myString.size(); ++i)
{
    //means that the character is nonwhitespace, so the string must contain other characters
    if(isspace(myString[i]) == 0)
    {
         stringHasCharacters = true;
         break;
    }
}

The thing was is that we havent learned that yet so I cannot use it, we have only used basic things.
It came to my head during lunch today and I got it.
The whole deal was the fact that "" || " " denoted a new line, yea as simple as it is, that was it. Heres the prog:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
  ifstream inFile("file.txt1");
  ofstream outFile("file.txt2");
string inputStr;
string leadstr;
leadstr = "     ";
 while(inFile)
   {
    getline(inFile,inputStr);
    outFile << leadstr << inputStr << endl;
    if (inputStr == " " || inputStr == "")
     {
       leadstr = "     ";
     }
    else
     {
       leadstr = "";
     }
   }

  inFile.close();
  outFile.close();
  return 0;
}

Again, thanks for the help, I appreciate it.

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.