Alright i'm trying to write a program that checks a list of names entered by the user to see if the new name entered is part of the list. If it's not part of the list it should return -1 to the main, and print no names found, if it is part of the list, it should return 0 and print the position of the name.
My issue that I'm having is I'll type a list of names, and then type a name to search, and unless that name is the first name on the list, it says name not found. Also, if you look at the code you'll notice that I'm printing "name found" rather then the string postion, and thats simply because I haven't figured out how to do that yet lol :). Help with this delemma would be greatly appreciated.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX_NAMELENGTH 10
#define MAX_NAMES 5
void initialize(char names[MAX_NAMES][MAX_NAMELENGTH]);
int search(char names[MAX_NAMES][MAX_NAMELENGTH],int i, int Number_entrys);
int main()
{
char names[MAX_NAMES][MAX_NAMELENGTH];
int i, Number_entrys,search_result,x;
initialize(names);
search_result= search(names,i,Number_entrys);
if (search_result==-1){
printf("Found no names.\n");
}
if(search_result==0){
printf("Name found");
}
getch();
return 0;
}
void initialize(char names[MAX_NAMES][MAX_NAMELENGTH])
{
int i,Number_entrys;
printf("How many names would you like to enter to the list?\n");
scanf("%d",&Number_entrys);
if(Number_entrys>MAX_NAMES){
printf("Please choose a smaller entry\n");
}else{
for (i=0; i<Number_entrys;i++){
scanf("%s",names[i]);
}
}
}
int search(char names[MAX_NAMES][MAX_NAMELENGTH],int i,int Number_entrys)
{
int j,n,x;
char new_name[MAX_NAMELENGTH];
printf("Now enter a name in which you would like to search the list for\n");
scanf("%s",new_name);
for(x = 0; x < Number_entrys; x++) {
if ( strcmp( new_name, names[x] ) == 0 ){
return x;
}else{
return -1;
}
}
}