searching for a record

This is a discussion on searching for a record within the C++ Programming forums, part of the General Programming Boards category; Let's say we have something like char year [50][5] and char event [50][15] . How would I search the year ...

  1. #1
    DVF
    DVF is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    searching for a record

    Let's say we have something like char year [50][5] and char event [50][15] . How would I search the year array for a particular input from the user so that it would display the year and the event for what the user entered. Or display a no-match found error, if there is no record found.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Loop through the year array, using strcmp(). If you find a match, record the index number (eg. the iteration of the for loop). When the loop is finished test the variable to see if it contains a valid index. You could default the initial value to a number outside the range, so you can tell if the year has been found. If the variable contains a valid index print the year and corresponding event using this index number else print the not-found message.

  3. #3
    DVF
    DVF is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    I been trying to do that, expect I have no idea how to test the variable after the loop. And I end up getting the first year array record getting printed. Damn there isn't even a single example using strcmp in the book.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    > I have no idea how to test the variable after the loop

    int var=-1;

    //your loop
    //look up strcmp
    //if match is found
    //set var to index
    //number


    if(var==-1)
    //year wasn't found
    else
    //print year and event

  5. #5
    DVF
    DVF is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    Well this is what I came up, but it prints out only the first year. Still confused.

    cout<<"Enter the year you're searching for\n";
    cin>> keyword;
    for (int Index = 0;(Index < 20)&&(found ==0); Index++)
    {
    if(strcmp(year[Index], keyword)) {found =1;}


    cout<<year[Index]<<setw(8)<<event[Index];



    }

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    strcmp returns zero if the strings match.

    int found=-1;
    cout<<"Enter the year you're searching for\n";
    cin>> keyword;
    for (int Index = 0;Index < 20; Index++)
    {
    if(strcmp(year[Index], keyword)==0) {found =Index;}
    }

    if(found!=-1)
    cout<<year[found]<<setw(8)<<event[found];
    else
    cout << "not found";

    *not tested*

  7. #7
    DVF
    DVF is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    That kinda solved the problem. Though still can't make to print the error message.

    Thanx a lot bro.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 09:41 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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