using namespace std;

struct karmand{
    char nam[10];
    char shomare[10];
    char sx[10];
    char mm[10];
    char rgh[10];
    }list[s];
void search();
void prin(int i);
void input_file();
void enter();
int a;
void menu_select(){
     char p[10];
    int c;
    system("cls");
    printf("1)enter a record:\n");
    printf("2)sort the file:\n");
    printf("3)search the file:\n");
    printf("4)quit:\n");

    do{
        printf("please enter your choice:");
        gets(p);
        c=atoi(p);
    }while(c<0||c>4);

        system("cls");
        switch(c){

            case 1:enter();break;
            case 2:search();break;


            case 3:exit(1);


}
}

void load(){
   FILE *fp;

   if((fp=fopen("df.txt","r+"))==NULL){
        printf("cannot open file");
        getch();
        exit(1);
    }
    int z=0;
   while(!feof(fp)){

      fscanf(fp,"%s",&list[z].nam);
      fscanf(fp,"%s",&list[z].shomare);
      fscanf(fp,"%s",&list[z].sx);
      fscanf(fp,"%s",&list[z].mm);
      fscanf(fp,"%s",&list[z].rgh);
      z++;
      a=z;
   }
  fclose(fp);

}



void search(){
    char e[20];
    int i;

    printf("please enter jostoju:");
    gets(e);
    for(i=0;strlen(list[i].nam)!=0;i++){
        if(strcmp(e,list[i].nam)==0){
           prin(i);

        }
        else{
          printf("cannot find");
       }
    }



}

void prin(int i){
    printf("%s",list[i].nam);
    printf("\t");
    printf("%s",list[i].shomare);
    printf("\t");
    printf("%s",list[i].sx);
    printf("\t");
    printf("%s",list[i].mm);
    printf("\t");
    printf("%s",list[i].rgh);
    printf("\n");

}

void enter(){

      printf("enter name khanevadegi:");
        scanf("%s",list[a].nam);
        printf("\nenter shomare personeli:");
        scanf("%s",list[a].shomare);
        printf("\nenter jensiat:");
        scanf("%s",list[a].sx);
        printf("\nenter vaziate ta'ahol:");
        scanf("%s",list[a].mm);
        printf("\nenter vaziate estekhdam:");
        scanf("%s",list[a].rgh);

        input_file();

}

void input_file(){
     FILE *out;
      if((out=fopen("df.txt","w+"))==NULL){
        printf("cannot open file");
        getch();
        exit(1);
    }
     for(int y=0;strlen(list[y].nam)!=0;y++){

       fwrite(&list[y],sizeof(struct karmand),1,out);
       fprintf(out,"\n");
     }
     fclose(out);
}

int main()
{
    load();
    enter();

   menu_select();
    return 0;
}

What is this:

using namespace std;

struct karmand{
    char nam[10];
    char shomare[10];
    char sx[10];
    char mm[10];
    char rgh[10];
    }list[s]; /*  s = ??? */

Don't you mean to code:

struct Contact
{
    // your contact info gets saved here
} ;

const int MAX_SIZE = 100;

Contact MyContacts[MAX_SIZE];

Your code seems to be C code (except for the 'using namespace std;' line ... maybe best to re-post as all C code in the C forum?

Also, do not use conio (cls, getch, etc) if you want portable code.

Best to always avoid dangerous use of gets.

In C++ use getline and a C++ string ...

In C use fgets
( or my fixedFgets
or my C emulation of C++ getline, called readLine )
(free for asking)

Also ...

What does NOT work right?

(What is your code supposed to do?
vs
What does it presently do?)

Does it compile error free?
(If not, please supply error messages.)

Edited 2 Years Ago by David W

This article has been dead for over six months. Start a new discussion instead.