| | |
Help with indenting infile -> outfile paragraph...
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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:
Any ideas?
Thank you
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:
C++ Syntax (Toggle Plain Text)
string inputStr; while(inFile) { getline(inFile,inputStr); if (!'\n') { outFile << " " << inputStr << endl; } else { outFile << inputStr << endl; } }
Any ideas?
Thank you
Last edited by R6er; Mar 26th, 2007 at 10:36 pm.
I don't know what you are trying to achieve by doing
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.
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
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
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
//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 }
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
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
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
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
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().
C++ Syntax (Toggle Plain Text)
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; } }
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
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
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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:
Again, thanks for the help, I appreciate it.
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:
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: hi
- Next Thread: System.String.LastIndexOf problem
Views: 2646 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






