very simple database program

This is a discussion on very simple database program within the C Programming forums, part of the General Programming Boards category; hello everyone, I've searched these forums and I've been lurking here for a while, so its time to post my ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    very simple database program

    hello everyone, I've searched these forums and I've been lurking here for a while, so its time to post my query.
    I hope im not breaking any rules here, simply I need a little help on my partial code that does not fully work. here goes.

    my code is part of a database which (surprise surprise!) adds, deletes, searches, and edits records of members. anyway enough of the talks, here's the part that i cant figure what im doing wrong
    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <stdlib.h>
    # include <string.h>
    
    # define MAX 100  /* constant*/
    
    struct addr {                  /*struct called list*/
                 char name[30]   ;
                 char street[40] ;
                 char town[20]   ;
                 char county[20] ;
                 char code[10]   ;
                }
                list[MAX];       /* 100 records*/
    
    void menu_select(void);
    void enter(void);
    void find_free(void);
    void inputs( char, char, int);
    
    int main(void)
    {
            clrscr();
            menu_select();
            getch();
            return 0;
    }
    
    void menu_select()
    {
        int choice;
        printf("1. Enter a name\n")  ;
        printf("2. Delete a name\n") ;
        printf("3. List the File \n");
        printf("4. Search\n")        ;
        printf("5. Save the file\n") ;
        printf("6. Load the file\n") ;
        printf("7. Quit\n")          ;
    
        do {
    	printf("\nEnter your choice: ");
           }
    	while(choice<0 || choice>7);
    
             scanf("%d",&choice);
    
          switch(choice)
           {
          	        case 1: enter();         /* enter a new entry */
          		break;
           //	case 2: del();           /* delete an entry */
          //		break;
          //	case 3: show_list();     /* display the list */
          //		break;
           //	case 4: search();        /* find an entry */
           //		break;
          //	case 5: save();          /* save to disk */
          //		break;
           //	case 6: load();          /* read from disk */
           //		break;
                    case 7: exit(0);
    
    	default:
    		printf("Invalid choice. Please enter again");
          }
          getch();
    }
    
    void enter()
    {
        int i;
    
        for(;;)
         {
          i = find_free();
          if(i<0)
          {
    	printf("list full\n");
    	return;
          }
          inputs("enter name: ", list[i].name,30);
          if
            (!*list[i].name) break;
          inputs("enter street: ", list[i].street, 40);
          inputs("enter town: ", list[i].town, 20);
          inputs("enter county: ", list[i].county, 20);
          inputs("enter Postal code: ", list[i].code, 10);
        }
    }
    
    void find_free()
    {
        register int i;
        for(i=0; i<MAX; i++)
          if(!*list[i].name)
          return i;
          return -1;
    }
    
    void inputs( char *prompt , char *s , int count)
    {
        char str[255];
    
        do
        {
    	printf(prompt);
    	gets(str);
    	if
              (strlen(str)>=count)
              printf("\ntoo long\n");
        }
        while(strlen(str)>=count);
    
        strcpy(s , str);
    }
    as you can see I'm only tryiing to sort out the enter function but i cant link it well enough with the other functions that it needs to use. can anyone help please? I've done my best. I know i might get a few negative repsonses about why i cant do it myself, if thats the case, then nevermind thanks in advance.
    Last edited by another_newbie; 05-24-2004 at 05:21 PM. Reason: tags not added

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You missed this all important post:
    << !! Posting Code? Read this First !! >>
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>and for some od reason i have written this code with tabs and branched it well, tho it doesnt seem to be displayed properly, im sorry about this<<
    Read the link I've just posted.

    Now to the code:

    >>void inputs(char, char, int);
    The first parameter is incorrect, you meant to have
    >>void inputs(char*, char*, int);

    >>void find_free(void);
    If you want to return something from a function, it can't be declared "void". I'd suggest
    >>int find_free(void);

    >>while (choice < 0 || choice > 7);
    What is value of choice the first time through the loop?
    What changes the value of choice within the loop? Look closely.

    Get it to compile, and post again when you have troubles.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    3
    Code:
    {
        int choice;
        char s[80];
    
        printf("1. Enter a name\n")  ;
        printf("2. Delete a name\n") ;
        printf("3. List the File \n");
        printf("4. Search\n")        ;
        printf("5. Save the file\n") ;
        printf("6. Load the file\n") ;
        printf("7. Quit\n")          ;
    
            printf("\nEnter your choice: ");
            gets(s);
            choice = atoi(s);
    ok i removed the do while loop which means im allowed to enter the details, but it does not exit after 1 go, it loops, and i dont seem to know where its looping. its late and i think i need sleep lol. so for now goodnight and thanks for the help. Much appreciated.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>and i dont seem to know where its looping.
    Neither do I, as you haven't posted anything that makes up a loop in your last post.
    The simplest way to debug is to put a load of printf() statements at key points in your code to see where the program is at.

    Anyway, don't use gets(), use fgets(). There's an entry in the faq telling you why
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    atoi() isn't much better, when you're feeling up to it, read this one:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    3
    dont worry i found out where it was looping constantly. it was in the
    Code:
              (for;;)
    in the void enter(void) function. there is still a lot more i need to sort out, but it seems to be much better than at the beginning. here's the code so far:
    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <stdlib.h>
    # include <string.h>
    
    # define MAX 100  /* constant*/
    
    struct addr {                  /*struct called list*/
                 char name[30]   ;
                 char street[40] ;
                 char town[20]   ;
                 char county[20] ;
                 char code[10]   ;
                }
                list[MAX];       /* 100 records*/
    
    void menu_select(void);
    void enter(void);
    int find_free(void);
    void inputs( char*, char*, int);
    
    int main(void)
    {
            clrscr();
            menu_select();
            getch();
            return 0;
    }
    
    void menu_select()
    {
        int choice;
        char s[80];
    
        printf("1. Enter a name\n")  ;
        printf("2. Delete a name\n") ;
        printf("3. List the File \n");
        printf("4. Search\n")        ;
        printf("5. Save the file\n") ;
        printf("6. Load the file\n") ;
        printf("7. Quit\n")          ;
    
            printf("\nEnter your choice: ");
            gets(s);
            choice = atoi(s);
    
          switch(choice)
           {
          	        case 1: enter();         /* enter a new entry */
          		break;
           //	case 2: del();           /* delete an entry */
           //		break;
           //	case 3: show_list();     /* display the list */
           //		break;
           //	case 4: search();        /* find an entry */
           //		break;
           //	case 5: save();          /* save to disk */
           //		break;
           //	case 6: load();          /* read from disk */
           //		break;
                    case 7: exit(0);
    
    	default:
    		printf("Invalid choice. Please enter again");
          }
          getch();
    }
    
    void enter()
    {
        int i;
    
        i = find_free();
          if(i<0)
          {
    	printf("list full\n");
    	return;
          }
          inputs("enter name: ", list[i].name,30);
         // if
          //  (!*list[i].name) break;
          inputs("enter street: ", list[i].street, 40);
          inputs("enter town: ", list[i].town, 20);
          inputs("enter county: ", list[i].county, 20);
          inputs("enter Postal code: ", list[i].code, 10);
    }
    
    
    int find_free()
    {
        register int i;
        for(i=0; i<MAX; i++)
          if(!*list[i].name)
          return i;
          return -1;
    }
    
    void inputs( char *prompt , char *s , int count)
    {
        char str[255];
    
        do
        {
    	printf(prompt);
    	gets(str);
    	if
              (strlen(str)>=count)
              printf("\ntoo long\n");
        }
        while(strlen(str)>=count);
    
        strcpy(s , str);
    }
    if anyone wants to compile this code then feel free, thats what its there for, but it wont work well enough so feel free to add comments, goodnight!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you're still after help, you're going to have to be a bit more specific with your questions. There's a variety of things you can do to improve your code, for example the gets() thing I've already mentioned. The rest is up to you at present, until you get really stuck
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-27-2005, 08:50 PM
  2. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. Creating a simple database: is my approach OK?
    By m712 in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2002, 01:27 AM
  5. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21