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 >
}
}
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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 );
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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 ();
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
So I guess the only option left is to manually parse the string character by character and draw your own results. Best of luck.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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;
}
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734