How do i repeat this search?

I'm just wondering how to make this code repeat itself with another string afterwards.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream myfile("t.txt");
    string a = "String";

    string search  = a;

    bool found = false;
    while(std::getline(myfile, a) && !found)
    {
        if(a.find(search) != string::npos)
        {
            cout << "Found a" << endl;
            found = true;
        }
    }
cin.get();
}


I tried this: But it wouldn't work correctly for me.

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

using namespace std;

int main()
{
    ifstream myfile("t.txt");
    string a = "String";
    string b = "Yeah";

    string search  = a;

    bool found = false;
    while(std::getline(myfile, a) && !found)
    {
        if(a.find(search) != string::npos)
        {
            cout << "Found a" << endl;
            found = true;
        }
    }
    {

    string search  = b;

    bool found = false;
    while(std::getline(myfile, b) && !found)
    {
        if(a.find(search) != string::npos)
        {
            cout << "Found b" << endl;
            found = true;
        }
    }}
cin.get();
}


Thank you!
Why don't you let the user input the search term instead of hard coding it ?
Also it would be much more efficient if you read and store the file in a string and only search the string.
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
#include<iostream>
#include<string>
#include <sstream>
#include<fstream>

using namespace std;

// please note that error checking is omitted
string read_file(const char *filename)
{
  stringstream buffer;

  buffer << ifstream(filename).rdbuf();

  return buffer.str();
}


int main()
{
  string needle;
  string haystack = read_file("your filename");
  for (;;)
  {
    cout << "Enter search term (quit to finish) : ";
    getline(cin, needle);
    if (needle == "quit")
      break;

    if (haystack.find(needle) != string::npos)
      cout << "Found.";
    else
      cout << "Not found.";
  }
}

Adding to Thomas1965,
You could try this pseudocode,
1. Put all strings to be searched in a container, i would use std::vector<std::string> searches;

2. Read file content as suggested by Thomas1965 (string content = read_file("your filename");)

3. Make a variable, count, to keep track of how many times you have taken something from searches

4. Take a string from searches (searching)
4.1. Increment count by 1

5. Start search from pos 0 of read file(content)
5.1 If the string (searching) is found, say string is found otherwise say string is not found
5.2 Set iterator of content to the beginning (i = 0);

6. Repeat 4 to 5.2 until count = searches.size();

5.1 can be modified: if found, remove searching from searches. In such a way, you could print out strings that were not found. Or, found strings could be put in a different containter say std::vector<std::string> foundStrings;

I hope my thoughts would be helpful. Try that lets see.
Last edited on
I dont let the user enter stuff because i need it to specifically search multiple strings that are predefined..
Even easier then. Put all the search words into a vector and use a loop.
Something like this, i think is what you want
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 * @file	readSearchString.cpp.
 * @date	2018-06-06
 * @author	blongho
 * @brief	Reads contents from a file and does search to see if content contains certain values
 */

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
std::string read_file(const char *filename); // read content
bool stringIsFound(const std::string & searching, const std::string & content);

int main()
{
	//std::string readFile = read_file("your file name");
	// sample read file
	std::string readFile{"\tThe greatest scientist that ever existed is not known.\n\t"
		"Some people call him God, others call him Allah. Among those that call\n\t"
		"him God, some even think that he is Yahweh, Messaih, and a lot more.\n\t"
		"If you gave him this same problem he would have said search for this\n\t"
		"in this and that will happen, just as he said \"Let there be light and"
		"there was light\".\n"
	};
	std::cout << readFile << std::endl;
	
	std::vector<std::string> searches{};
	searches.push_back("god"); // note this is not same as God. 
	searches.push_back("Allah");
	searches.push_back("people");
	searches.push_back("Einstein");
	searches.push_back("Moobman");

	std::vector<std::string> foundStrings{};
	std::vector<std::string> notFoundStrings{};
	
	for (auto &search : searches){
		if (stringIsFound(search, readFile)){
			std::cout << "\t" << search << " is found" << std::endl;
			// process found string
			//foundStrings.push_back(search); 
		}
		else{
			std::cout << "\t" << search << " is not found" << std::endl;
			// process not found string here or later
			//notFoundStrings.push_back(search);
		}
	}

	std::cout << std::endl;
	return 0;
}

/**
 * @fn	bool stringIsFound(const std::string & searching, const std::string & content)
 *
 * @brief	String is found
 * @param	searching	The searching string.
 * @param	content  	The content.
 * @return	True if it succeeds, false if it fails.
 */

bool stringIsFound(const std::string & searching, const std::string & content)
{
	return content.find(searching) !=std::string::npos;
}

/**
 * @fn	std::string read_file(const char *filename)
 * @brief	please note that error checking is omitted
 * @param	filename	Filename of the file.
 * @return	The file as a string.
 */

std::string read_file(const char *filename)
{
	std::stringstream buffer;

	buffer << std::ifstream(filename).rdbuf();

	return buffer.str();
}
Last edited on
Registered users can post here. Sign in or register to post.