4 Replies - 1005 Views - Last Post: 30 September 2013 - 06:51 AM Rate Topic: -----

#1 siddiqui_1985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-March 13

Database program using structure and files in C

Posted 30 September 2013 - 06:01 AM

Hi everyone
i have a problem in creating a basic database program in C(Turbo C) using structures and files.
here is my main function

int main()
{
    int key,key1;
    int nextPos =1;
    int PrevPos;
    system("cls");
    menu();
    gotoxy(20,6);
    // i am facing problem i this while loop nothing happend 
    //when i used switch it works for writing data to the file and showing next recode in the file but not previouse 
    while((key = key_chk()) != 24)
    {
        if(key == 1)
        {
            do
            {
                system("cls");
                menu();
                writeData();
                printf("\n\nAdd more data y/n? : ");
            }while(getchar() == 'y');
        }
        if(key == 3) // checks if user want to see data
        {
            key1 = key_chk();
            if(key1 == 77) //if user press right arrow key display next data in a file
            {
                system("cls");
                menu();
                readNext(nextPos);
                nextPos++;
                PrevPos = nextPos;
            }
            if(key1 == 75)//if user press left arrow key go back to previous data in a file
            {
                system("cls");
                menu();
                readPrev(PrevPos);
                PrevPos--;
            }
            nextPos = PrevPos;
        }
    }
getch();
return 0;
}




any help would be appreciated.
Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Database program using structure and files in C

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,236
  • Joined: 05-May 12

Re: Database program using structure and files in C

Posted 30 September 2013 - 06:11 AM

Also posted here: http://www.daniweb.c...se-program-in-c
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Database program using structure and files in C

Posted 30 September 2013 - 06:15 AM

ALL your options should be either in menu(), or inside the function that is being called from the menu(). For example, say I want to add another record. I choose Add from the menu(), now the program jumps to the add() function, and I enter the data I want for this record, and confirm it's OK. Now I return from the add() function, and I'm back in the menu() function again.

You don't want something in main() calling menu() over and over, and jumping around from there to all different functions. That's what the menu function is for. You have no doubt noticed the excessive amount of duplicated code in this section - a sure sign a better design of your program, is needed.
Was This Post Helpful? 0
  • +
  • -

#4 siddiqui_1985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-March 13

Re: Database program using structure and files in C

Posted 30 September 2013 - 06:21 AM

hmm will try that. and sorry @skydiver no one replied on daniweb so posted it here :)

@adak i should call everything in menu and menu in main right ?
Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Database program using structure and files in C

Posted 30 September 2013 - 06:51 AM

View Postsiddiqui_1985, on 30 September 2013 - 06:21 AM, said:

hmm will try that. and sorry @skydiver no one replied on daniweb so posted it here :)/>

@adak i should call everything in menu and menu in main right ?

Yes. You have a menu displayed in menu() and right below the display (in the code), you can use a large switch statement. Based on the user's entry, the switch statement calls out the correct function the user requests. Some will call multiple functions, of course.

For instance, if the user wants to delete a record, that can't be done unless you first locate that record, so search() is called, (either in the menu switch code, or in delete() ).

About the only thing you need (or might want to do), in main() is open the data file, and read it into your data structure you're using, then call main(), and say good by at the end.

My video library menu() looks like this:
void menu(void) {
   char ch;
   char buff[80];
   int i,n, index, ok=0;
  
   do {
      //clear the menu area of the display
      _gotoxy(1,1);
      //for(i=0;i<3;i++) {
      //printf("                                                                              *\n"); 
      //}
      printf("                                                                               \n");  
      _gotoxy(1,2);
      printf("                <<<<      Welcome to the Main Menu       >>>>                  \n");
      printf("                                                                               \n"); 
      printf("        [a] Add a video            [d] Delete a video                          \n");
      printf("        [e] Edit a video record    [s] Search for a video                      \n");
      printf("        [v] View all videos        [w] Write out all videos                    \n");
      printf("                                                                              *\n");
      printf("                     Your Choice, or Q to quit:                                \n");
      printf("                                                                              *\n"); 
      //i=_wherey();
      _gotoxy(49,8);    //row 8 is the Choice row in the menu
      fgets(buff, sizeof(buff), stdin);
      sscanf(&buff[0],"%c",&ch);
      switch (ch) {
         case 'a':   
            printf("\n Would you like to add a new video? [y/n]: ");
            ch = getchar();
            getchar();
            if(ch=='y' || ch=='Y') {
               n=addVideos();
               if(n) {   //a successful add
                  sortRecords();
                  _gotoxy(1,12);
                  printf(" One Video has been added. %d Videos in all now.                    **\n",NumVideos);
                  getchar(); 
               }
            }
            _gotoxy(1,12);
           printf("                                                                              *\n"); 
            break;
         case 'd':  break;
            ok=idelete(); 
            if(ok)
               --NumVideos;
            break;
      case 'e': edit();  //8888888888888888888888
                break;    
      case 's':  
         for(i=0;i<10;i++) {
            printf("                                                                              *\n"); 
         }
         _gotoxy(1,10);   
         printf("Enter the video's  \n  title: ");
         fgets(buff, sizeof(videos[0]), stdin); 
         buff[strlen(buff)-1]='\0';
         index = fetch(buff);
         //printf("Index: %d\n",index); getchar();
         if(index>-1) {
            printf(" #%d) title: %s\n  actors: %s\n   genra: %s\n  rating: %d\n\n",
                    index+1,videos[index].title,videos[index].actor,videos[index].genra,videos[index].rating);
            printf("                                                                              *\n"); 
            printf("                                                                              *\n"); 
            printf("                                                                              *\n"); 
            printf("                                                                              *\n"); 
            getchar();
         }
         break;
      case 'v':  view(); break;
      case 'w':  writeAll(); break;

      case 'Q':  ch='q';
      case 'q':  break;
      default: printf("\n\t\t    Please enter a valid selection\n");
    };

  }while(ch!='q'); 
}



As you can see, it hasn't been finished, but you get the idea. A large do while or while loop, with the menu options displayed, then a large switch statement below, to handle the various calls to the other functions. Everything loops back through here, until the user wants to quit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1