Help with indenting infile -> outfile paragraph...

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

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
  #11
Mar 26th, 2007
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:

  1. string inputStr;
  2. while(inFile)
  3. {
  4. getline(inFile,inputStr);
  5.  
  6. if (!'\n')
  7. {
  8. outFile << " " << inputStr << endl;
  9. }
  10. else
  11. {
  12. outFile << inputStr << endl;
  13. }
  14. }

Any ideas?
Thank you
Last edited by R6er; Mar 26th, 2007 at 10:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
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: 474
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
  #12
Mar 26th, 2007
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.
Last edited by ~s.o.s~; Mar 26th, 2007 at 10:52 pm.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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
  #13
Mar 26th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
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: 474
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
  #14
Mar 26th, 2007
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.
  1. //check for a blank line
  2. if(find_first_not_of(" \t\r\f\n\v") == string::npos)
  3. {
  4. //write a indent to the new file
  5. }
  6. else
  7. {
  8. // write the line as it is
  9. }
Last edited by ~s.o.s~; Mar 26th, 2007 at 11:35 pm.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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
  #15
Mar 26th, 2007
Interesting, but no we have not.

Im still plugging away to no avail YET.

Thanks for the thought.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
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: 474
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
  #16
Mar 26th, 2007
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 don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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
  #17
Mar 26th, 2007
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.
Last edited by R6er; Mar 27th, 2007 at 12:00 am.
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
  #18
Mar 28th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
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: 474
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
  #19
Mar 28th, 2007
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().
  1. for(int i = 0; i < myString.size(); ++i)
  2. {
  3. //means that the character is nonwhitespace, so the string must contain other characters
  4. if(isspace(myString[i]) == 0)
  5. {
  6. stringHasCharacters = true;
  7. break;
  8. }
  9. }
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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
  #20
Mar 28th, 2007
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:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. int main ()
  6. {
  7. ifstream inFile("file.txt1");
  8. ofstream outFile("file.txt2");
  9. string inputStr;
  10. string leadstr;
  11. leadstr = " ";
  12. while(inFile)
  13. {
  14. getline(inFile,inputStr);
  15. outFile << leadstr << inputStr << endl;
  16. if (inputStr == " " || inputStr == "")
  17. {
  18. leadstr = " ";
  19. }
  20. else
  21. {
  22. leadstr = "";
  23. }
  24. }
  25.  
  26. inFile.close();
  27. outFile.close();
  28. return 0;
  29. }

Again, thanks for the help, I appreciate it.
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


Views: 2646 | Replies: 19
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC