Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This program is supposed to have an array of 10 string objects that hold people's names and phone numbers. It asks the user to enter a name or partial name to search for in the array, and any entries in the array that match the string entered should be displayed. I'm having problems executing the code.

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

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    const int size = 50;
    char name[50]; 
    int count;

    char list[] = {"Becky Warren, 555-1223",
                   "Joe Looney, 555-0097",
                   "Geri Palmer, 555-8787",
                   "Lynn Presnell, 555-8878", 
                   "Holly Gaddis, 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"};


    cout << "Enter a name or partial name: " << endl;
    cin.getline(name, size);
    cin.ignore();

    for(count = 0; count < 10 ; count++){
        if(strstr(list[count], name)){
            cout << list[count];
        }
    }

    cin.get();
    return 0;
}
share|improve this question
5  
What problems are you having? –  Björn Pollex Dec 15 '11 at 8:27
1  
First, there are eleven entries in your array. Secondly, strstr is case sensitive, so "Becky" is not equal to "becky". –  Joachim Pileborg Dec 15 '11 at 8:29
 
Why are you using C strings in a C++ program ? –  Paul R Dec 15 '11 at 8:30
 
Which test input is failing? –  Vinayak Garg Dec 15 '11 at 8:31

closed as not a real question by casperOne Oct 23 '12 at 12:07

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

First, your data declaration is wrong. You don't want an array of char, you want an array of strings, either std::string or char const* (the latter only if all of the strings are literals). Secondly, you probably don't want the table to be an auto variable; unless you want to modify it later in the function, and reinitializing it each time the function is called, you probably want it to be static:

static char const* const list[] =
{
    "Becky Warren, 555-1223",
    "Joe Looney, 555-0097",
    "Geri Palmer, 555-8787",
    "Lynn Presnell, 555-8878", 
    "Holly Gaddis, 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",
};

Otherwise, you want to use std::vector and std::string, with something like the above to initialize it:

std::vector<std::string> localList( std::begin( list ),
                                    std::end( list ) );

(For your example code, this isn't necessary. You can just use list.)

Second, you should use the free function std::getline, and read into an std::string, so you don't have to worry about size. And I don't think you want the cin.ignore(). This will try to extract (and ignore) one extra character after the new line, and will probably require entering an additional new line (since the system won't send any characters to your program until there is a new line).

And you don't want to use an explicit number of elements to manage the loop, but rather something derived from the size of the array itself; iterators would be the most generic:

for ( char const* const* iter = std::begin( list );
        iter != std::end( list );
        ++ iter ) {
    if ( std::search( *iter, *iter + strlen( *iter ),
                      name.begin(), name.end() )
            != *iter + strlen( *iter ) ) {
        std::cout << *iter << std::endl;
}

This is, of course, case sensitive; in a real application, you'd probably write your own matcher (a functional object), which would implement the exact matching criteria; similarly, you would use std::find_if, something like:

Matcher m( name );
char const* const* iter = std::find_if(
                    std::begin( list ), std::end( list ), m );
while ( iter != std::end( list ) ) {
    std::cout << *iter << std::endl;
    iter = std::find_if( iter + 1, std::end( list ), m );
}
share|improve this answer

Minor modifications done to suit my compiler, this code works. Corrected declaration of 2 dimensional char list . Running here with hardcoded input : http://codepad.org/ZVkbLjHU

  #include <iostream>
  #include <cstring>
  #include <string>
  using namespace std;

int main(int argc)
{
  const int size = 50;
  char name[50];
  int count;
  char list[][50] = {"Becky Warren, 555-1223",
                 "Joe Looney, 555-0097",
                 "Geri Palmer, 555-8787",
                 "Lynn Presnell, 555-8878",
                 "Holly Gaddis, 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" };

  cout << "Enter a name or partial name: " << endl;
  cin.getline(name, size);
  cin.ignore();
  for(count = 0; count < 10 ; count++){
    if(strstr(list[count], name)){
      cout << list[count];
    }
  }
  cin.get();
  return 0;
}
share|improve this answer
1  
What's the point of int const size = 50; if you then use 50 everywhere? And do you really want list on the stack, initialized dynamically? And the cin.ignore() doesn't look right either. –  James Kanze Dec 15 '11 at 9:24
 
@JamesKanze , you are right. I just posted a quick fix to his code with minimal editing. –  DhruvPathak Dec 15 '11 at 10:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.