Phone Number List issues

Ok, So technically this program works. My problem is my error message is also displayed when I print the information. Can someone help me figure out how to prevent this from happening?
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
/*****************************************
Phone Number List
******************************************/

# include <iostream>		// i/o stream header
# include <cstring>			// header file needed to use strstr function
using namespace std;

/******************
Function Main
******************/
int main ()
{
	const int num = 11;		// num holds 11 contacts
	const int length = 100;		// Maximum allowable charactersfor each line is 100

	//  Array contacts[][] holds 11 contacts with phone numbers
	char contacts[num][length] = {   "Becky Warren, 555-1223", 
									 "Joe Looney, 555-0097",
									 "Geri Palmer, 555-8787", 
									 "Lynn Presnell, 555-1212",
									 "Holly Gaddis, 555-8878", 
									 "Sam Wiggins, 555-0998",
									 "Bob Kain, 555-8712", 
									 "Tim Haynes, 555-7676",
									 "Warren Gaddis, 555-9037", 
									 "Jean James, 555-4949",
									 "Ron Palmer, 555-2783"};

	char search[length];		//Array search[] holds user's input
								//and searches inside the array contacts[][]
	char *IdContact = NULL;		// IdContact points to the user's query inside array contacts[][]
	int index;		// loop counter
	
	//Prompts user to search for contacts
	cout << "To search for your contact's number please enter a name or partial name of the person.\n";
	cin.getline(search, length);		//user's input
	
	//Searches array for matching substring
	for (index = 0; index < num; index++)
	{
		IdContact = strstr(contacts[index], search);
			if (IdContact != NULL)
			{
				cout << contacts[index];		//prints all possible contacts with in the string
				
			}
			
	}

	//if no matches are found, display error message;
	if (IdContact == NULL)
				cout << "Sorry, No matches were found!";
	return 0;
}


Here you go

Basically, when search reached end, IdContact was Null again. So you have to tell the program that we already found matches. I put

bool found = false;

When you find a match, found = true.

if(!found) // if found == false
{
cout << "No matches!" << endl;
}

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
/*****************************************
Phone Number List
******************************************/

# include <iostream>		// i/o stream header
# include <cstring>			// header file needed to use strstr function
using namespace std;

/******************
Function Main
******************/
int main ()
{
    bool found = false;
	const int num = 11;		// num holds 11 contacts
	const int length = 100;		// Maximum allowable charactersfor each line is 100

	//  Array contacts[][] holds 11 contacts with phone numbers
	char contacts[num][length] = {   "Becky Warren, 555-1223",
									 "Joe Looney, 555-0097",
									 "Geri Palmer, 555-8787",
									 "Lynn Presnell, 555-1212",
									 "Holly Gaddis, 555-8878",
									 "Sam Wiggins, 555-0998",
									 "Bob Kain, 555-8712",
									 "Tim Haynes, 555-7676",
									 "Warren Gaddis, 555-9037",
									 "Jean James, 555-4949",
									 "Ron Palmer, 555-2783"};

	char search[length];		//Array search[] holds user's input
								//and searches inside the array contacts[][]
	char *IdContact = NULL;		// IdContact points to the user's query inside array contacts[][]
	int index;		// loop counter

	//Prompts user to search for contacts
	cout << "To search for your contact's number \nplease enter a name or partial name of the person.\n";
	cin.getline(search, length);		//user's input

	//Searches array for matching substring
	for (index = 0; index < num; index++)
	{
		IdContact = strstr(contacts[index], search);
        if (IdContact != NULL)
        {
            cout << contacts[index] << endl;		//prints all possible contacts with in the string
            found = true;
        }
	}
	//if no matches are found, display error message;
	if (!found) cout << "Sorry, No matches were found!";

	return 0;
}
Yep! That fix it alright! Thank you very much! I find I under use the bool data types. You definitely need it for this problem. Can you explain why the other way did not work for the error message?
Well, ex. It finds matches but there is nothing to tell the program that it did find some matches so that is why I put in bool found. In the original program, the program just continued on, you could just put an else after but in this case, you didn't.
Topic archived. No new replies allowed.