Not getting good result searching an array of string.

This is a discussion on Not getting good result searching an array of string. within the C++ Programming forums, part of the General Programming Boards category; OS: Win 2k Compiler: MS VC++ 6.0 Maybe my logic isn't good enough. I am trying to search through an ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Unhappy Not getting good result searching an array of string.

    OS: Win 2k Compiler: MS VC++ 6.0

    Maybe my logic isn't good enough. I am trying to search through an array of strings for a name to see whwther or nor it exists. The problem is even though the name is clearly in the list, the program says it's not. Is my logic wrong?
    After answering Y, the program wouldn't allow me to continue processing (selecting another name) if name is not in the list.
    Please see my codes and the result I got:

    The unsorted names are:
    C a r l B i l l i o n
    S m i t h B i r t
    A l J i m
    A l b e r t J i m
    S t a n K a r t y
    R o s e A m r e
    B a y T e r r
    J o h n s o n E r a b
    A l l i s o n C a r r
    J e a n J o e y
    W i l l B i l l
    J a m e s G a r t

    The sorted names are:
    Al Jim
    Albert Jim
    Allison Carr
    Bay Terr
    Carl Billion
    James Gart
    Jean Joey
    Johnson Erab
    Rose Amre
    Smith Birt
    Stan Karty
    Will Bill
    ------------------
    Performing a search-------

    Please enter a name to search for:
    Stan Karty
    This name is not found
    Another selection? (Y or N) Y
    Press any key to continue
    -------- The codes ------
    --------------------------
    /*
    Purpose: Sort in alphabetical order a list of names first 0r last) using an array of chars.
    */
    #include <iostream>
    #include <cstring>
    using namespace std;

    #define ELEMENTCOUNT 12

    void showArray(char strings[][17], int);
    typedef int (*cmpfunc) (const void*, const void*);

    int main()
    {
    char found[17];
    char go;
    char (strings[12][17]) = {
    "Carl Billion", "Smith Birt", "Al Jim",
    "Albert Jim", "Stan Karty", "Rose Amre",
    "Bay Terr", "Johnson Erab", "Allison Carr",
    "Jean Joey", "Will Bill", "James Gart"};

    cout << "The unsorted names are: \n";
    showArray(strings, 12);
    cout << "\n";

    char searchedName[17];
    qsort(strings, 12, 17, (cmpfunc)strcmp);
    cout << "The sorted names are: \n";

    for( int i = 0; i < 12; i++ )
    {
    // puts( strings[i] );
    cout << (strings[i]) <<endl;
    }
    cout << "------------------\n";
    cout << "Performing a search-------\n" << endl;
    //do
    //{
    cout << "Please enter a name to search for: \n";
    cin.getline(searchedName, 17);
    //not used -->found = strlen(searchedName);
    strcpy(found, searchedName); // error C2106 points to this line of code
    char *srchNamePtr = found;
    if (srchNamePtr == searchedName)
    {
    strings,12,17,(cmpfunc)strcmp);
    cout << "The name: " << searchedName[17] << "is found" << '\n' << endl;
    }
    else
    {
    cout << "This name is not found\n";
    do
    {
    cout << "Another selection? (Y or N) ";
    cin >> go;
    if (toupper(go) == 'y') //not used -->|| toupper(go) == 'n')
    {
    cin.getline(searchedName, 17);
    }
    else
    {return 0;}
    }
    while ( (go !='y') || (go == 'Y') );
    //not used: while (toupper(go) !='Y' && toupper(go) !='N');
    } // end of else
    return 0;
    } // end of main()
    // function definitions ==========================
    void showArray(char strings[12][17], int elems)
    {
    for (int i = 0; i < elems; i++)
    {
    for ( int j = 0 ; j < elems; j++)
    cout << strings[i][j]<<" ";
    cout << endl;
    }
    cout<<endl<<endl;
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    srchNamePtr == searchedName

    You cannot compare strings this way. Use strcmp.

    Remove the syntactical errors. This is not the code that you posted the output of.

    Go through your array in a loop using strcmp on each member and your search string. If you found one, break the loop and display it.

    Hint: Use CODE tags when posting like this:

    Code:
    [ code ]
    main()
    {
       blah
    }
    [ /code ]
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Code:
    i second that hint
    Blue

  4. #4
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    use strcmp if you want case sensativity, and strcmpi if not

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Unhappy Still bad result in searching a string array

    Yes, I did use the correct code (strcmp) but I am not getting the correct result. The old problem still persist; when I type in a name that don't exist the program tells me that the name is in the array when it's not. I think you should review that particular section again and let me know what you have found. The sort is still fine though. See the changes.
    -----------------------------------------------------
    #include <iostream>
    #include <cstring>
    #include <ctype.h>
    using namespace std;

    #define ELEMENTCOUNT 12

    void showArray(char strings[][17], int);
    typedef int (*cmpfunc) (const void*, const void*);

    int main()
    {
    char found[17];
    char go;
    char (strings[12][17]) = {
    "Carl Billion", "Smith Birt", "Al Jim",
    "Albert Jim", "Stan Karty", "Rose Amre",
    "Bay Terr", "Johnson Erab", "Allison Carr",
    "Jean Joey", "Will Bill", "James Gart"};

    cout << "The unsorted names are: \n";
    showArray(strings, 12);
    cout << "\n";

    char searchedName[17];
    qsort(strings, 12, 17, (cmpfunc)strcmp);
    cout << "The sorted names are: \n";

    for( int i = 0; i < 12; i++ )
    {
    // puts( strings[i] );
    cout << (strings[i]) <<endl;
    }
    cout << "------------------\n";
    cout << "Performing a search-------\n" << endl;
    cout << "Please enter a name to search for: \n";
    cin.getline(searchedName, 17);
    char *srchNamePtr = found;
    if(strcmp(srchNamePtr, searchedName) == 0)
    {
    cout << "The name: " << searchedName[17] << "is found" << '\n' << endl;
    }
    else
    {
    cout << "This name is not found\n";
    do
    {
    cout << "Another selection? (Y or N) ";
    cin >> go;
    if (toupper(go) == 'y')
    {
    cin.getline(searchedName, 17);
    }
    else
    {return 0;}
    }
    while ( (go !='y') || (go == 'Y') );
    } // end of else
    return 0;
    } // end of main()
    // function definitions =======================
    void showArray(char strings[12][17], int elems)
    {
    for (int i = 0; i < elems; i++)
    {
    for ( int j = 0 ; j < elems; j++)
    cout << strings[i][j]<<" ";
    cout << endl;
    }
    cout<<endl<<endl;
    }

  6. #6
    Unregistered
    Guest
    I suspect the conundrum has to do with what happens when strcmp() tries to compare a valid string with an invalid string.

    srchNamePtr is not a valid string when used as a parameter in the call to strcmp() because it is not initialized. You have passed it the address of the first element of the variable found, but the variable found is in itself not initialized. Therefore when strcmp tires to compare it to a valid string it may well return 0. Therefore 0 == 0 so the following statement is run.

    I suggest dropping the variable srchNamePtr and the variable found.

    the comparison with strcmp should be this;

    strcmp(strings[i], searchedName)

    Also the request for user input to find a name should be outside the loop that does the actual search. The way you have it the user will need to enter the name each time the computer moves to a new string in the array to compare to--ands that's not user friendly.

    =========================================

    BTW the following line should cause major problems, too:

    cout << "The name: " << searchedName[17] << "is found" << '\n' << endl;


    The problem with this line is that searchedName is declared to be a char array with 17 elements, but searchedName[17] as used above says display the 18th element of searchedName-----which doesn't exist. Drop the [17] in the above line to get it correct---you want to display the string, not a single char, and certainly not a char with an out of bounds array index.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21