String Search

Hello,

I am facing some type of logical error.
My program is for chapter 12 challenge 6 for Starting out with C++ 9th edition.
The logical error is when after I enter a non-existing file when I try to input a new word and the file name the word counter is at 0.
I am trying to read the textbook. I am really lost and not sure what to do.

Can anyone point me to the right direction.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

// The size amount of the array
const int NAME_SIZE = 54;

// Main test driver
int main()
{
string input;
char wordTracker[NAME_SIZE];
string fileName;
fstream textReader;
int counter = 0;
string findWord;
size_t resultCollect;

// Ask the user to input a word(string) from the file
cout << "Enter a word or a set of words in a string: ";
cin.getline(wordTracker, NAME_SIZE);

// Ask the user to input the file's name for access
cout << "Please enter the name of the file: ";
getline(cin, fileName);
cout << "Enter the string from the file to be searched: ";
getline(cin, findWord);
cout << endl;
// Using an fstream object to open the file
textReader.open(fileName, ios::in);

// If the user enters a non-existing file an error occurs
if (textReader.fail())
{
cout << "Error this file doesn't exist" << endl;
cout << "Please enter another file name: ";
getline(cin, fileName);
cout << "Enter the string from the file to be searched: ";
getline(cin, findWord);
}
// Using the while loop
while(getline(textReader, input))
{
resultCollect = input.find(findWord);
while(resultCollect!=string::npos)
{ counter++;
resultCollect = input.find(findWord, resultCollect + 1);
}
}
cout << "The word appear # of times: " << counter << endl;
// Closing the file
textReader.close();
system("PAUSE");
return 0;
}
Last edited on
You have a number of logical errors in your program.

Line 34: If the open of the file fails, you ask the user for another file name, then ignore what the user entered. Don't you think you should try the open again with the new file name?

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

I wasn't sure.
What logical errors speficially can you point them out.

I pointed out one logical error above. Asking the user for a new file name, then ignoring what the user entered. As a user, that would annoy me.

Logical error two. If the open fails, you fall through to the while loop at line 43 and call getline.
Guess what? If the file failed to open, your call to getline is going to fail. the while loop is not going to execute and you fall through to the cout at line 52 which of course is going to report zero words found.
As formatted:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

// The size amount of the array
const int NAME_SIZE = 54;

// Main test driver
int main() {
	string input;
	char wordTracker[NAME_SIZE];
	string fileName;
	fstream textReader;
	int counter = 0;
	string findWord;
	size_t resultCollect;

	// Ask the user to input a word(string) from the file
	cout << "Enter a word or a set of words in a string: ";
	cin.getline(wordTracker, NAME_SIZE);

	// Ask the user to input the file's name for access
	cout << "Please enter the name of the file: ";
	getline(cin, fileName);

	cout << "Enter the string from the file to be searched: ";
	getline(cin, findWord);
	cout << endl;

	// Using an fstream object to open the file
	textReader.open(fileName, ios::in);

	// If the user enters a non-existing file an error occurs
	if (textReader.fail()) {
		cout << "Error this file doesn't exist" << endl;
		cout << "Please enter another file name: ";
		getline(cin, fileName);
		cout << "Enter the string from the file to be searched: ";
		getline(cin, findWord);
	}

	// Using the while loop
	while (getline(textReader, input)) {
		resultCollect = input.find(findWord);
		while (resultCollect != string::npos) {
			counter++;
			resultCollect = input.find(findWord, resultCollect + 1);
		}
	}

	cout << "The word appear # of times: " << counter << endl;
	// Closing the file
	textReader.close();
	system("PAUSE");
	return 0;
}

What is wordTracker supposed to be used for - as it's not used after it's value is obtained???

Possibly:

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
#include <iostream>
#include <string>
#include <fstream>

int main() {
	std::string fileName;
	std::string findWord;

	std::cout << "Please enter the name of the file: ";
	std::getline(std::cin, fileName);

	std::ifstream textReader(fileName);

	// If the user enters a non-existing file an error occurs
	while (!textReader) {
		std::cout << "Error this file doesn't exist\n";
		std::cout << "Please enter another file name: ";
		std::getline(std::cin, fileName);
		textReader.open(fileName);
	}

	std::cout << "Enter the string from the file to be searched: ";
	std::getline(std::cin, findWord);
	std::cout << '\n';
	size_t counter {};

	for (std::string input; getline(textReader, input); )
		for (size_t resultCollect {}; (resultCollect = input.find(findWord, resultCollect)) != std::string::npos; ++counter, ++resultCollect);

	std::cout << "The word appears " << counter << " times(s)\n";
}

I am gone a bit over board when I written some of my variables.
Which probably is what is causing issues for my program I had dealt a fail stream object after like inputting a file which didn't exist to ensure it displayed the error message.
The problem that I had encountered I had modified a bit by changing the if statement to a while loop. I eliminated the wordTracker variable and also NAME_SIZE variable since I wasn't sure what to do with those.
To safe space on my code I declared another function called textSearch to deal with searching for the file name if there is an error or trying to list the occurences of a word in a spefic text file.
The issue now is that it over counts a word.
For my List.txt file when I ran it into my program I typed a word such as murder which should get 0 but for some reason it got 37.
I will try to look at the feedback and see if what @seeplus may have fixed
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
[#include <iostream>
#include <string>
#include <fstream>
using namespace std;

// Function prototype
void textSearch(fstream &textReader, string fileName)
{
    string findWord;
    string input;
    int counter = 0; 
    size_t resultCollect;
    // If the user enters a non-existing file an error occurs
    while (textReader.fail())
    {
	textReader.clear();
	cout << "Error this file doesn't exist" << endl;
    cout << "Please enter another file name: ";
    getline(cin, fileName);
    textReader.open(fileName, ios::in);
    cout << "Enter the string from the file to be searched: ";
    getline(cin, findWord);
	}
    // Using the while loop
    while(getline(textReader, input))
    {
       resultCollect = input.find(findWord);
       while(resultCollect!=string::npos)
        {         counter++;
        resultCollect = input.find(findWord, resultCollect + 1);
	    }
	}
    cout << "The word appear # of times: " << counter << endl;
}

// Main test driver
int main()
{
	string input; 
    string fileName;
    fstream textReader;
    string findWord;
    
    // Ask the user to input the file's name for access
    cout << "Please enter the name of the file: ";
    getline(cin, fileName);
    cout << "Enter the string from the file to be searched: ";
    getline(cin, findWord);
    cout << endl;
	// Using an fstream object to open the file
    textReader.open(fileName, ios::in);
    
    // Function call 
    textSearch(textReader, fileName);
    // Closing the file
    textReader.close();
    system("PAUSE");
	return 0;   
}


Last edited on
Topic archived. No new replies allowed.