hOW TO SEARCH STRING BEYOND STRING

I am given a list of 30 names and I'm require to write a program to allow user to search by word/words..
For exp:
John
Michelle
Michael
.
.
.
.
.
.

When user type 'm', the name of Michelle and Michael will listed out.
When user type 'hn', the name John wil listed out.
Hi,
Do you bring the code with you?
closed account (E0p9LyTq)
std::string::find
http://www.cplusplus.com/reference/string/string/find/
@closed account 5a8Ym39o6
@FurryGuy


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
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

int main(void)
{
	int characterorintegal;

		cout << "Please enter a character or integal" << endl;
	cin >> characterorintegal;

	        str0 = 16 / 07 / 2016        John              NewYork          Fever            Steve         30 
		str1 = 16 / 07 / 2016        Michael           NewYork          Food Poisoning   Mike          40  
		str2 = 16 / 07 / 2016        Michelle          Washington       Flu              Steve         50
		str3 = 16 / 07 / 2016        Jack              NewYork          Fever            Mike          30
		str4 = 16 / 07 / 2016        Jackky            California       Fever            Steve         70
		str5 = 16 / 07 / 2016        Malaysia          California       Fever            Mike          50


		// Search ( I don't know how to write, Pls help)


		cout << "cout << left << setw(12) << "Date" << setw(16) << "Name" << setw(16) << "Address(City)" << setw(8) << "Illness" << setw(7) << "Doctor" << setw(6) << "Charge" << endl;"
		cout << str << endl;

	return 0;
}
Last edited on
@closed account 5a8Ym39o6
@FurryGuy

It's something like this.
closed account (E0p9LyTq)
Here's a start in searching in strings:

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

int main()
{
   std::string str[] = { "16 / 07 / 2016        John              NewYork          Fever            Steve         30",
                         "16 / 07 / 2016        Michael           NewYork          Food Poisoning   Mike          40",
                         "16 / 07 / 2016        Michelle          Washington       Flu              Steve         50",
                         "16 / 07 / 2016        Jack              NewYork          Fever            Mike          30",
                         "16 / 07 / 2016        Jackky            California       Fever            Steve         70",
                         "16 / 07 / 2016        Malaysia          California       Fever            Mike          50" };

   std::cout << "Enter your search string: ";
   std::string search;
   std::getline(std::cin, search);

   for (int i = 0; i < 6; i++)
   {
      std::string::size_type found = str[i].find(search);
 
      if (found != std::string::npos)
      {
         std::cout << "first '" << search << "' found at: " << found
            <<  " in string[" << i << "]\n";
      }
   }
}


Enter your search string: ever
first 'ever' found at: 58 in string[0]
first 'ever' found at: 58 in string[3]
first 'ever' found at: 58 in string[4]
first 'ever' found at: 58 in string[5]
Last edited on
Topic archived. No new replies allowed.