Help with functions in class

Hi there, as below code, I am having difficulty with display function.

As you can see that, I have use while loop to scanText() then included processText() functions to filter unnecessary texts within the loop.

Unfortunately, I tried to call displayText() function in main, it shows the last word of texts regardless there are many texts.

Of course I could include displayText() within while loop, but this action will defeat the purpose to call displayText(), since scanText() can be used to show the output as well.

Can anyone please help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
string WordApp::scanText(string filename)
{
	ifstream reader;

	reader.open(filename + ".txt");

	if (reader.is_open())
	{
		while (!reader.eof())
		{
			reader >> text;
			processText(text);
			output = processText(text);
		}
		reader.close();
	}
	else
	{
		cout << "File not found, please try again..." << endl;
		system("PAUSE");
		exit(1);
	}
	return text;
}

string WordApp::processText(string result) //change to private
{
	char special[] = "`'~!@#$%^&*()_\'-=+{}[]:;?/.,<>";
	transform(result.begin(), result.end(), result.begin(), ::tolower);

	for (unsigned int i = 0; +i < strlen(special); ++i)
	{
		result.erase(remove(result.begin(), result.end(), special[i]), result.end());
	}
	return result;
}

void WordApp::displayText()
{
	cout << output << endl;
}
Last edited on
On line 13 you store each processed word in the same variable, thus overriding the previous one.
Could you explain in plain English what you actually want to do.
Thanks so much
Topic archived. No new replies allowed.