You know I gave up trying to understand what you are trying to do there, so instead, I give you something to play with and hopefully you can figure out for yourself what you're trying to do.
But first things first! Don't make your strings so damn small!
"char x[4]" type stuff is just a disaster waiting to happen.
Second, understand that the number one rule in programming is "hand - to - hand" passing of variables. Your program is not psychic. There must be a physical connection between components...
Third, remember that you have to fill in anything that will be printed and/or processed. Empty variables == crash - OK?
Anyway, good luck....
The following progam does NOT answer your question!! But it will run and you can modify it, as necessary. You will have to be more clear on your question to get more detailed help...
Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <conio.c> //...needed for clrscr()
//void sortarray(struct manufacturer manu[],int length);
int matches(char *s, char *t)
{
if( strstr(s, t) )
return 1;
else
return 0;
}
void find(struct manufacturer manu[],int length );
//************prototype******************
struct manufacturer
{
char code[20];
char name[20];
char address[50];
char contact[20];
int position;
};
//*********m a i n *****************
int main(void)
{
char temp[20];
int i;
int MAX;
printf("How many employees to enter today?:\n");
fgets(temp, 20, stdin);
MAX = atoi( temp );
if( MAX == 0)
return 0;
struct manufacturer man[ MAX ];
for(i=0; i< MAX; i++)
{
printf("please enter a name:\n");
fgets(man[i].name, 20, stdin);
printf("please enter his position:\n");
fgets(temp, 20 ,stdin);
man[i].position = atoi( temp ); //...convert string to integer...
printf("please enter the job code:\n");
fgets(man[i].code, 20, stdin);
printf("please enter the worker's address:\n");
fgets(man[i].address, 50, stdin);
printf("please enter a contact number:\n");
fgets(man[i].contact, 20, stdin);
clrscr();
}
find(man, MAX );
return(0);
}
//**************function****************
void find(struct manufacturer manu[],int length)
{
#define FALSE 0
#define TRUE 1
char choice[100];
int i;
int again = TRUE;
int found;
do{
printf("please enter a code to search for, 'X' to exit:\n");
fgets(choice, 100, stdin);
if( matches( choice, "x" ) || matches(choice, "X") )
again = FALSE;
found = FALSE; //...reset variable...
for(i=0; i<length && again == TRUE ; i++)
{
if( matches(manu[i].code, choice) )
{
found = TRUE;
printf("Matches for search : %s\n", choice);
printf("name: %s\n", manu[i].name);
printf("position: %d\n\n", manu[i].position);
printf("address: %s\n", manu[i].address);
printf("contact: %s\n", manu[i].contact);
getch();
clrscr();
}
}
if( found == FALSE && again == TRUE)
{
printf("\nSorry, no matches for that code\n");
getch();
}
clrscr();
}while(again);
}