Iam suppose to make a program that creates an employee database of names, addresses, and phone numbers. the user to search by name for an employee in the database. if the name is not found output a message to that effect.

so far I have this but its giving me problems when I run the program. An screen pops up and says that there was a problem with the cmd.exe

then again in the program I know Iam missing something but what is it????/

#include <stdio.h>

struct database
{
       char Name[36];
       char Address[46];
       char Phone[15];
};

int compare_strings (char s1[], char s2[])
{
  int i=0, answer;

  while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0') ++i;

  if ( s1[i] < s2[i])
     answer = -1;
  else if ( s1[i] == s2[i])
     answer = 0;
  else
      answer = 1;

  return (answer);
}

int lookup (struct database employee[], char search[],int entries)
{
    int low =0;
    int high = entries -1;
    int mid, result;
    int compare_strings (char s1[], char s2[]);

    while (low <= high)
    {
          mid = (low + high) / 2;
          result = compare_strings (employee[mid].Name, search);

          if ( result == -1)
             low = mid +1;
          else if (result == 1)
               high = mid - 1;
          else 
               return (mid);
    }      
}

  main()
  {

   struct  database employee [100] = 
          {  {"Lisa ", "4300 sloping oak"  },
             {"Fernando", "2300 clearmeot" },
             {"juan", "5300 cleasmer"      },
             {"Victor", "4200 terase"      } };

       int entries = 10;
       char Name[10];
       int entry_number;
       int lookup (struct database employee[], char search[], int entries );

       printf ("Name to search: ");
       scanf ("%9s",Name);

       entry_number = lookup (employee, Name, entries);

       if (entry_number != -1)
          printf ("%s\n", employee[entry_number].Address);
       else
           printf ("Name is not found.\n");

           system("pause");       
    }

if you have two strings you can use strcmp(char*str1, char*str2) which is a standard function. the cmd error is probably from 'bad data', ie when you initialised the database you have got 4 entries defined and the rest is random junk (uninitialised). as you have told the program you have 10 entries it will include the junk in the program data and is likely to cause errors. It would be a GREAT idea to make a constructor for the database structure so that it sets strings to NULL on initialisation, the problem should then dissapear... just a quick thought :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.