Hi,
I wrote a program that reads a file, by building up text one character at a time, looks for keywords, and replaces them if they are found. I am working on building up my output. The words come out right, and I added the punctuation back in ok. The last step is, if there is a return in the original text that started a new line, then I need to start a new line in the output. I tried,

if (text[j] == '\n'){
cout << endl;
}

Recommended Answers

All 4 Replies

can't help you without the code because I can't see your monitor from my chair.

Sorry

while (!potter.eof()){
			string word_in = "";
			getline(potter, text);
			for (int j=0; j < (int)text.length(); j++){
				if (isalpha (text[j])){
					word_in += text[j];
				}
				else {
					int flag = 0;
					for (int i=0; i<count; i++){
						if (word_in == word[i]){
							flag = 1;
							newtext[i] = subs[i];
							counters[i]++;
							break;
						}
					}
					if (flag == 1){
						cout << subs[i];
						output << subs[i];
					}
					if (flag == 0){
						cout << word_in;
						output << word_in;
					}
					if (!isalpha (text[j])){
						cout << text[j];
						output << text[j];
					}
					if (text[j] == '\n'){
						cout << endl;
					}
					else {
						cout << " ";
						output << " ";
					}

it's a long code, so this is only the part that outputs the solution. Flag 0 outputs the original word, Flag 1 outputs the substited word. The isalpha statement inserts the punctionation back it, the else statement adds spaces. The problem is the text[j] == '\n' statement, it doesnt work. Text j is an array formed by taking in the characters one at a time.

line 1 is wrong -- eof() doesn't work the way you think it does. Here's the correction (combin line 1 and 3)

while ( getline(potter, text) ){

The getline() function does not put the '\n' in the input buffer, so the test on line 30 will never ever be true. The solution is to just display '\n' after that loop terminated.

Thank you very much, that works.

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.