Help with indenting infile -> outfile paragraph...

Thread Solved
Reply

Join Date: Mar 2007
Posts: 15
Reputation: R6er is an unknown quantity at this point 
Solved Threads: 0
R6er R6er is offline Offline
Newbie Poster

Help with indenting infile -> outfile paragraph...

 
0
  #1
Mar 24th, 2007
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:

  1. while(inFile)
  2. {
  3. getline(inFile,inputStr);
  4.  
  5. if ()
  6. {
  7. outFile << " " << inputStr << endl;
  8. }
  9. else
  10. {
  11. outFile << inputStr << endl;
  12. }
  13. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Help with indenting infile -> outfile paragraph...

 
0
  #2
Mar 24th, 2007
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
  1. while( getline(inFile,inputStr) )
  2. {
  3. if ( <flag is set> )
  4. {
  5. outFile << " " << inputStr << endl;
  6. <set flag to false>
  7. }
  8. else
  9. {
  10. outFile << inputStr << endl;
  11. <if inputStr is an empty string set flag to true >
  12. }
  13. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 15
Reputation: R6er is an unknown quantity at this point 
Solved Threads: 0
R6er R6er is offline Offline
Newbie Poster

Re: Help with indenting infile -> outfile paragraph...

 
0
  #3
Mar 24th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,151
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Help with indenting infile -> outfile paragraph...

 
0
  #4
Mar 25th, 2007
>>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.
  1. inputStr == "";
  2.  
  3. or
  4.  
  5. inputStr.length() == 0
  6.  
  7. or
  8. 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.
Last edited by Ancient Dragon; Mar 25th, 2007 at 12:12 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help with indenting infile -> outfile paragraph...

 
0
  #5
Mar 25th, 2007
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):
  1. #include <string>
  2.  
  3. const std::string whiteSpaces( " \f\n\r\t\v" );
  4.  
  5. void trimRight( std::string& str,
  6. const std::string& trimChars = whiteSpaces )
  7. {
  8. std::string::size_type pos = str.find_last_not_of( trimChars );
  9. str.erase( pos + 1 );
  10.  
  11. }
  12.  
  13.  
  14. void trimLeft( std::string& str,
  15. const std::string& trimChars = whiteSpaces )
  16. {
  17. std::string::size_type pos = str.find_first_not_of( trimChars );
  18. str.erase( 0, pos );
  19. }
  20.  
  21.  
  22. void trim( std::string& str, const std::string& trimChars = whiteSpaces )
  23. {
  24. trimRight( str, trimChars );
  25. trimLeft( str, trimChars );
  26. }
Last edited by ~s.o.s~; Mar 25th, 2007 at 12:31 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 15
Reputation: R6er is an unknown quantity at this point 
Solved Threads: 0
R6er R6er is offline Offline
Newbie Poster

Re: Help with indenting infile -> outfile paragraph...

 
0
  #6
Mar 25th, 2007
Thank you for taking the time to give me some hints here.
I have taken your tips into consideration and have formulated this here:

  1. isSafe = true;
  2. while(getline(inFile,inputStr))
  3. {
  4. if(isSafe)
  5. {
  6. outFile << " " <<inputStr <<endl;
  7. isSafe = false;
  8. }
  9. else if ((inputStr == "") || (inputStr.length() ==0) || (inputStr.size() ==0))
  10. {
  11. isSafe = true;
  12. }
  13. else
  14. {
  15. outFile << inputStr << endl;
  16. }
  17.  
  18. }

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help with indenting infile -> outfile paragraph...

 
0
  #7
Mar 25th, 2007
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)

  1. void leftTrim(string& inStr, const string delimiters = " \t\n\r\v\f")
  2. {
  3. string::size_type pos = inStr.find_first_not_of(delimiters);
  4. inStr.erase(0, pos);
  5. }
  6.  
  7. int main ()
  8. {
  9. string line;
  10. bool flag = true;
  11. ifstream inFile("test.txt");
  12. ofstream outFile("output.txt");
  13. while(getline(inFile, line))
  14. {
  15. leftTrim(line);
  16. if(line.size() == 0)
  17. {
  18. flag = true;
  19. continue; //skip current line and move to next one
  20. }
  21. if(flag) //if a new paragraph is afoot
  22. {
  23. outFile << endl << " " << line << endl;
  24. flag = false;
  25. }
  26. else //its a normal line
  27. {
  28. outFile << line << endl;
  29. }
  30. }
  31. //close the streams and wait for keypress
  32. inFile.close();
  33. outFile.close();
  34. getchar ();
  35. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 15
Reputation: R6er is an unknown quantity at this point 
Solved Threads: 0
R6er R6er is offline Offline
Newbie Poster

Re: Help with indenting infile -> outfile paragraph...

 
0
  #8
Mar 25th, 2007
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:

  1. getchar ();

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

Thanks again
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help with indenting infile -> outfile paragraph...

 
0
  #9
Mar 25th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 15
Reputation: R6er is an unknown quantity at this point 
Solved Threads: 0
R6er R6er is offline Offline
Newbie Poster

Re: Help with indenting infile -> outfile paragraph...

 
0
  #10
Mar 25th, 2007
That makes sence, I am suprized that I have not run into that just yet.
Thanks for the tip.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC