Need to get my program to print multiple names

I can get the program to print out one name but I need it to be able to print out duplicates. Any help is always appreciated.





#include "stdafx.h"
#include<string>
#include<iostream>
#include<cstring>

using namespace std;

//prototype



int main()
{

bool foundNames = false; //Fix for names never showing
//Constants for array lengths
const int TELE_NUM = 15;
const int LENGTH = 30;

//Array of contact names/numbers
char contacts[TELE_NUM][LENGTH] =
{
"Alejandra Cruz, 555-1223",
"Joey Looney, 555-0997",
"Geri Palmer, 555-8787",
"Li Chen, 555-1212",
"Holly Gadis, 555-8878",
"Sam Wiggins, 555-0998",
"Bob Kain, 555- 8712",
"Tim Haynes, 555-7676",
"Warren Gaddis, 555-9037",
"Jean James, 555-4939",
"Ron Palmer, 555-2783"
};

char lookup[LENGTH]; //Holding users input
char *strPtr = nullptr; //To hold point to the found product
int index; //Loop counter

//Prompt user for the persons name/phone number
cout << "Enter the partial name or phone number of the person you're looking for. This is case sensitive \n";
cin.getline(lookup, LENGTH);

//Search the array for the matching substring
for (index = 0; index < TELE_NUM; index++)
{
strPtr=strstr(contacts[index], lookup);
if (strPtr != nullptr) //Actual fix for names never showing "!="
break;
}

//If there was a matching name or phone number display the results
if (strPtr != nullptr)
{
cout << contacts[index] << endl;
foundNames = true;
}
else
if(!foundNames)
cout << "There was no person or phone number that matched your input" << endl;

system("pause");
return 0;
}
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

Your 'search' stops on first match, does it not? Why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (index = 0; index < TELE_NUM; index++)
{
  strPtr = strstr( contacts[index], lookup );
  if (strPtr != nullptr)
  {
    cout << contacts[index] << endl;
    foundNames = true;
  }
}

if( !foundNames )
{
  cout << "There was no person or phone number that matched your input" << endl;
}



PS. Why do you include <string>, but don't use std::string anywhere?
Force of habit.
Also, I'm not sure how to display multiple names. That's why I asked.
Thanks.
To include <string> is a habit? How about developing a new habit to use std::string and to not use char arrays & C's string functions?
Thanks for the info.
Now are you going to answer my question?
I can get the program to print out one name but I need it to be able to print out duplicates.

Look for duplicates. When you find one, print it out.

Which of those two things is difficult for you?
Last edited on
The program isn't stopping on the first thing it finds.

It's going thru the entire list, and any time it finds a match, it remembers a pointer to that match. The program is probably reporting the LAST thing it finds.

Instead of recording a pointer then printing the info out after the loop, why not do the input within the loop? That's the simplest answer to the problem - just output the answers as they are found. No pointer needed.

If you have to use pointers, then you're going to need to store a list of them in an array, or something like that.
Now are you going to answer my question?

Which question? How to print all matching lines?

The code that I did post is not identical to what you did post. Mine should print them all.
Topic archived. No new replies allowed.