code problem

This is a discussion on code problem within the C Programming forums, part of the General Programming Boards category; hi, this code of mine works fine. Only thing is that when i add a new entry(not modify) windows xp ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    code problem

    hi, this code of mine works fine. Only thing is that when i add a new entry(not modify) windows xp brings this error message that the .exe file has caused an error, would you like to send an error message. I have tried compiling on two machines with 2 diff compilers, with the same results. could you guys help me debug it??? the tbfile is any text file that you can create with notepad. thanks a bunch
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_STRING_SIZE 81
    #define TEL_BOOK_SIZE 50
    
    struct tel_book_element
         {  char *name;
            char *telnum; };
    typedef struct tel_book_element TelBookElement;
    
    struct tel_book
         {  TelBookElement entry[TEL_BOOK_SIZE];
            int n; };
    typedef struct tel_book TelBook;
    
    // function protocols
    
    int fgetTelBook(TelBook *tbp);
    int menu(void);
    void sort(TelBook *tbp, int n);
    void lookName(TelBook *tbp, int n, int *ip);
    void lookNum(TelBook *tbp, int n);
    int insertNew(TelBook *tbp, int n, int *ip);
    int deleteEntry(TelBook *tbp, int n);
    void listNames(TelBook *tbp, int n);
    void outputData(TelBook *tbp, int n);
    char *fgetString(FILE *fp);
    int binarySearch(TelBook *tbp, int *ip, char searchName[], int n);
    void strToUpper(char *sp);
    
    //main function
    int main()
    {  TelBook tb, *tbp=&tb;   //variable declarations
      int n, i=0, *ip=&i;
      int choice=0;
    
      n=fgetTelBook(tbp);
      while (choice != 7)
      {  choice=menu();
         switch(choice)
         {
          case 1: sort(tbp,n); break;
          case 2: lookName(tbp,n,ip); break;
          case 3: lookNum(tbp,n); break;
          case 4: n=insertNew(tbp,n,ip); break;
          case 5: n=deleteEntry(tbp,n); break;
          case 6: listNames(tbp,n); break;
          case 7: outputData(tbp,n); break;
          default: printf("\n Please enter a valid selection: ");
                choice=getch();
                choice=toupper(choice);
                break;
         }
       }
    return (0);}
    // function definitions
    
    char *fgetString(FILE *fp)
    {  char s[MAX_STRING_SIZE], *sp;
      int n;
    
      if (fgets(s,MAX_STRING_SIZE,fp)==NULL) return NULL;
      n=strlen(s);
      if (s[n-1]=='\n') s[--n]='\0';
      sp=malloc(n+1);
      if(sp==NULL) { printf("malloc could not allocate storage for string\n");
                exit(1); }
      strcpy(sp,s);
      return(sp); }
    
    int fgetTelBook(TelBook *tbp)
    {  char *namep;
      FILE *fp;
      int n;
    
      if ((fp=fopen("tbfile.txt", "r"))==NULL)
         { printf("The \"tbfile.txt\" could not be opened.\n");
           exit(1); }
      n=0;
      namep=fgetString(fp);
      while (namep!=NULL)
         {  tbp -> entry[n].name=namep;
          tbp -> entry[n].telnum=fgetString(fp);
          namep=fgetString(fp);
          n++; }
      tbp -> entry[n].name='\0';
      tbp -> entry[n].telnum='\0';
      fclose(fp);
      return (n); }
    
    int menu(void)
    {  int choice;
      printf("Menu for Automated Telephone Book\n");
      printf("   1) Sort new telephone book\n");
      printf("   2) Look up name\n");
      printf("   3) Look up telephone number\n");
      printf("   4) Insert new/modified entry\n");
      printf("   5) Delete entry\n");
      printf("   6) List names\n");
      printf("   7) Quit\n");
      printf("Enter your selection: ");
      scanf(" %d", &choice);
    
      return (choice); }
    
    void strToUpper(char *sp)
    {  int n, j;
      n=strlen(sp);
      for (j=0; j<=n-1; j++) *(sp+j)=toupper(*(sp+j));
    }
    
    void sort(TelBook *tbp, int n)
     {  int j, k, pass;
        char temp_num[MAX_STRING_SIZE], hold[MAX_STRING_SIZE], *sp;
        for (j=0; j<=n-1; j++) {
                   sp=tbp->entry[j].name;
                   strToUpper(sp);}
        for (pass=1; pass<=n-1; pass++) {
          for (k=0; k<=n-2; k++) {
              if (strcmp(tbp->entry[k].name,tbp->entry[k+1].name) > 0)
              { hold[0]='\0';
                strcpy(hold,tbp-> entry[k].name);
                strcpy(tbp-> entry[k].name,tbp-> entry[k+1].name);
                strcpy(tbp-> entry[k+1].name, hold);
                strcpy(temp_num,tbp-> entry[k].telnum);
                strcpy(tbp-> entry[k].telnum, tbp-> entry[k+1].telnum);
                strcpy(tbp-> entry[k+1].telnum,temp_num); }}}}
    
    
    int binarySearch(TelBook *tbp, int *ip, char searchName[], int n)
    {  int low, high, middle;
      low=0; high=n-1;
      while (low <= high)
        {  middle=(low+high)/2;
         if (strcmp(searchName, tbp-> entry[middle].name) == 0)
           {  *ip=middle;
              return 1; }
         else if (strcmp(searchName, tbp-> entry[middle].name) < 0)
              high=middle-1;
              else low=middle+1;
              }
        *ip=low;
    
        return 0; }
    
    
    void lookName(TelBook *tbp, int n, int *ip)
    {  char c,searchName[81];
      int j=0,k;
    
      printf("Enter the name to look up: ");
      c=getchar();
      do { c=toupper(c);
         searchName[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       searchName[j]='\0';
      for (k=0;k<=j-2;k++) searchName[k]=searchName[k+1];
      searchName[k]='\0';
    
      if (binarySearch(tbp, ip, searchName,n)==1)
            printf("\n Telephone # is: %s\n\n", tbp->entry[*ip].telnum);
        else printf("\n Name not found- Check spelling of name.\n\n");
    }
    
    
    void lookNum(TelBook *tbp, int n)
    {  char c,searchNum[81];
      int pos=-1;
      int j=0,k;
    
      printf("Enter the number to look up: ");
      c=getc(stdin);
      do { c=toupper(c);
         searchNum[j]=c;
         j++;
         c=getc(stdin); } while (c!='\n');
       searchNum[j]='\0';
      for (k=0;k<=j-2;k++) searchNum[k]=searchNum[k+1];
      searchNum[k]='\0';
      for (j=0; j<=n-1; j++)
          if (strcmp(searchNum,tbp->entry[j].telnum)==0) pos=j;
      if (pos==-1) printf ("Telephone number not found.\n\n");
      else printf("That number belongs to %s.\n\n", tbp->entry[pos].name); }
    
    int insertNew(TelBook *tbp, int n, int *ip)
    {  char c, newname[81], newnum[81];
      int j=0,k;
      printf("Enter name: ");
      c=getchar();
      do { c=toupper(c);
         newname[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       newname[j]='\0';
      for (k=0;k<=j-2;k++) newname[k]=newname[k+1];
      newname[k]='\0';
    
      printf("Enter Number: ");
      j=0;
      c=getc(stdin);
      do { c=toupper(c);
         newnum[j]=c;
         j++;
         c=getc(stdin); } while (c!='\n');
       newnum[j]='\0';
    
      if (binarySearch(tbp, ip, newname,n))
         { strcpy(tbp->entry[*ip].telnum,newnum);
           printf("Old Entry Modified\n"); }
      else { for (j=n; j>=*ip; j--)
               { strcpy(tbp->entry[j].name, tbp-> entry[j-1].name);
              strcpy(tbp->entry[j].telnum, tbp-> entry[j-1].telnum); }
           free(tbp->entry[j].name);
           free(tbp->entry[j].telnum);
           strcpy(tbp-> entry[j].name,newname);
           strcpy(tbp-> entry[j].telnum,newnum);
           n++;
           printf("New entry inserted.\n"); }
    
      return n; }
    
    int deleteEntry(TelBook *tbp, int n)
    {  char *name, c;
      int i, *ip=&i, j=0, k;
    
      printf("Enter Name: ");
      c=getchar();
      do { c=toupper(c);
         name[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       name[j]='\0';
      for (k=0;k<=j-2;k++) name[k]=name[k+1];
      name[k]='\0';
      if (binarySearch(tbp, ip, name, n))
         { free(tbp->entry[i].name);
           free(tbp->entry[i].telnum);
           for (j=i; j<=n-2; j++)
                {  strcpy(tbp->entry[i].name,tbp->entry[i+1].name);
                   strcpy(tbp->entry[i].telnum,tbp->entry[i+1].telnum);}
           printf("Entry deleted. \n");
           n--;
         }
      else printf("Name not found.\n");
    return n;}
    
    
    void listNames(TelBook *tbp, int n)
    { char c, name[MAX_STRING_SIZE];
      int j=0, k;
    
      printf("Enter first part of name: ");
      c=getchar();
      do { c=toupper(c);
         name[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       name[j]='\0';
      for (k=0;k<=j-1;k++) name[k]=name[k+1];
      name[k]='\0';
      printf("\n");
      for (k=0;k<=n-1;k++)
         if (tbp->entry[k].name==strstr(tbp->entry[k].name,name))
              printf("%s: %s\n", tbp->entry[k].name, tbp->entry[k].telnum);
      printf("\n");
    }
    
    void outputData(TelBook *tbp, int n)
    { char *namep;
     FILE *fp;
     int j;
    
      if ((fp=fopen("tbfile.txt", "w"))==NULL)
         { printf("The \"tbfile.txt\" could not be opened.\n");
           exit(1); }
      for (j=0;j<=n-1; j++)
         {  fputs(tbp->entry[j].name,fp);
          fputc('\n',fp);
          fputs(tbp->entry[j].telnum,fp);
          fputc('\n',fp); }
      fclose(fp);
    
      }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>strcpy(tbp->entry[*ip].telnum,newnum);
    Did you allocate any memory for telnum? (it's only a pointer to start with, not an array).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    no i dont think so,
    all that dynamic allocation is the proff logic:-

    http://www.cs.uh.edu/~imangesh/assignment10.txt

    how do i do that????/

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use the same principles as mentioned in your paper:
    Note that you
    need to input the name and telephone number as dynamically allocated strings so use fgetString(stdin), e.g.

    char *newName;
    newName = fgetString(stdin); strToUpper(newNname);...
    free(tbp->entry[i].name); /*de-allocate storage for old name*/
    tbp->entry[i].name = newName;
    Slight modifications needed, obviously...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    so u mean to say that when the loop says that it is a new entry, i use fgetstring to allocate memory to the pointer?//

    edit:- for some reason the func fgetString is accepting stdin as a valid input.

    edit 2:- well the delete function has been fixed, but i still need help with inserting a new element.

    Code:
    int insertNew(TelBook *tbp, int n, int *ip)
    {  char c, newname[81], newnum[81];
      int j=0,k;
      printf("Enter name: ");
      c=getchar();
      do { c=toupper(c);
         newname[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       newname[j]='\0';
      for (k=0;k<=j-2;k++) newname[k]=newname[k+1];
      newname[k]='\0';
    
      printf("Enter Number: ");
      j=0;
      c=getc(stdin);
      do { c=toupper(c);
         newnum[j]=c;
         j++;
         c=getc(stdin); } while (c!='\n');
       newnum[j]='\0';
    
      if (binarySearch(tbp, ip, newname,n))
         { strcpy(tbp->entry[*ip].telnum,newnum);
           printf("Old Entry Modified\n"); }
      else { for (j=n; j>=*ip; j--)
               { strcpy(tbp->entry[j].name, tbp-> entry[j-1].name);
              strcpy(tbp->entry[j].telnum, tbp-> entry[j-1].telnum); }
           newname=fgetString(stdin);
           newnum=fgetString(stdin);
           free(tbp->entry[j].name);
           free(tbp->entry[j].telnum);
           strcpy(tbp-> entry[j].name,newname);
           strcpy(tbp-> entry[j].telnum,newnum);
           n++;
           printf("New entry inserted.\n"); }
    
      return n; }
    Last edited by kashifk; 04-22-2003 at 03:31 PM.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    the new updated code
    [code]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include<conio.h>
    #define MAX_STRING_SIZE 81
    #define TEL_BOOK_SIZE 50
    
    struct tel_book_element
         {  char *name;
            char *telnum; };
    typedef struct tel_book_element TelBookElement;
    
    struct tel_book
         {  TelBookElement entry[TEL_BOOK_SIZE];
            int n; };
    typedef struct tel_book TelBook;
    
    // function protocols
    
    int fgetTelBook(TelBook *tbp);
    int menu(void);
    void sort(TelBook *tbp, int n);
    void lookName(TelBook *tbp, int n, int *ip);
    void lookNum(TelBook *tbp, int n);
    int insertNew(TelBook *tbp, int n, int *ip);
    int deleteEntry(TelBook *tbp, int n);
    void listNames(TelBook *tbp, int n);
    void outputData(TelBook *tbp, int n);
    char *fgetString(FILE *fp);
    int binarySearch(TelBook *tbp, int *ip, char searchName[], int n);
    void strToUpper(char *sp);
    
    //main function
    int main()
    {  TelBook tb, *tbp=&tb;   //variable declarations
      int n, i=0, *ip=&i;
      int choice=0;
    
      n=fgetTelBook(tbp);
      while (choice != 7)
      {  choice=menu();
         switch(choice)
         {
          case 1: sort(tbp,n); break;
          case 2: lookName(tbp,n,ip); break;
          case 3: lookNum(tbp,n); break;
          case 4: n=insertNew(tbp,n,ip); break;
          case 5: n=deleteEntry(tbp,n); break;
          case 6: listNames(tbp,n); break;
          case 7: outputData(tbp,n); break;
          default: printf("\n Please enter a valid selection: ");
    		  scanf("%d",&choice);
                
                
                break;
         }
       }
    return (0);}
    // function definitions
    
    char *fgetString(FILE *fp)
    {  char s[MAX_STRING_SIZE], *sp;
      int n;
    
      if (fgets(s,MAX_STRING_SIZE,fp)==NULL) return NULL;
      n=strlen(s);
      if (s[n-1]=='\n') s[--n]='\0';
      sp=malloc(n+1);
      if(sp==NULL) { printf("malloc could not allocate storage for string\n");
                exit(1); }
      strcpy(sp,s);
      return(sp); }
    
    int fgetTelBook(TelBook *tbp)
    {  char *namep;
      FILE *fp;
      int n;
    
      if ((fp=fopen("tbfile.txt", "r"))==NULL)
         { printf("The \"tbfile.txt\" could not be opened.\n");
           exit(1); }
      n=0;
      namep=fgetString(fp);
      while (namep!=NULL)
         {  tbp -> entry[n].name=namep;
          tbp -> entry[n].telnum=fgetString(fp);
          namep=fgetString(fp);
          n++; }
      tbp -> entry[n].name='\0';
      tbp -> entry[n].telnum='\0';
      fclose(fp);
      return (n); }
    
    int menu(void)
    {  int choice;
      printf("Menu for Automated Telephone Book\n");
      printf("   1) Sort new telephone book\n");
      printf("   2) Look up name\n");
      printf("   3) Look up telephone number\n");
      printf("   4) Insert new/modified entry\n");
      printf("   5) Delete entry\n");
      printf("   6) List names\n");
      printf("   7) Quit\n");
      printf("Enter your selection: ");
      scanf(" %d", &choice);
    
      return (choice); }
    
    void strToUpper(char *sp)
    {  int n, j;
      n=strlen(sp);
      for (j=0; j<=n-1; j++) *(sp+j)=toupper(*(sp+j));
    }
    
    void sort(TelBook *tbp, int n)
     {  int j, k, pass;
        char temp_num[MAX_STRING_SIZE], hold[MAX_STRING_SIZE], *sp;
        for (j=0; j<=n-1; j++) {
                   sp=tbp->entry[j].name;
                   strToUpper(sp);}
        for (pass=1; pass<=n-1; pass++) {
          for (k=0; k<=n-2; k++) {
              if (strcmp(tbp->entry[k].name,tbp->entry[k+1].name) > 0)
              { hold[0]='\0';
                strcpy(hold,tbp-> entry[k].name);
                strcpy(tbp-> entry[k].name,tbp-> entry[k+1].name);
                strcpy(tbp-> entry[k+1].name, hold);
                strcpy(temp_num,tbp-> entry[k].telnum);
                strcpy(tbp-> entry[k].telnum, tbp-> entry[k+1].telnum);
                strcpy(tbp-> entry[k+1].telnum,temp_num); }}}}
    
    
    int binarySearch(TelBook *tbp, int *ip, char searchName[], int n)
    {  int low, high, middle;
      low=0; high=n-1;
      while (low <= high)
        {  middle=(low+high)/2;
         if (strcmp(searchName, tbp-> entry[middle].name) == 0)
           {  *ip=middle;
              return 1; }
         else if (strcmp(searchName, tbp-> entry[middle].name) < 0)
              high=middle-1;
              else low=middle+1;
              }
        *ip=low;
    
        return 0; }
    
    
    void lookName(TelBook *tbp, int n, int *ip)
    {  char c,searchName[81];
      int j=0,k;
    
      printf("Enter the name to look up: ");
      c=getchar();
      do { c=toupper(c);
         searchName[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       searchName[j]='\0';
      for (k=0;k<=j-2;k++) searchName[k]=searchName[k+1];
      searchName[k]='\0';
    
      if (binarySearch(tbp, ip, searchName,n)==1)
            printf("\n Telephone # is: %s\n\n", tbp->entry[*ip].telnum);
        else printf("\n Name not found- Check spelling of name.\n\n");
    }
    
    
    void lookNum(TelBook *tbp, int n)
    {  char c,searchNum[81];
      int pos=-1;
      int j=0,k;
    
      printf("Enter the number to look up: ");
      c=getc(stdin);
      do { c=toupper(c);
         searchNum[j]=c;
         j++;
         c=getc(stdin); } while (c!='\n');
       searchNum[j]='\0';
      for (k=0;k<=j-2;k++) searchNum[k]=searchNum[k+1];
      searchNum[k]='\0';
      for (j=0; j<=n-1; j++)
          if (strcmp(searchNum,tbp->entry[j].telnum)==0) pos=j;
      if (pos==-1) printf ("Telephone number not found.\n\n");
      else printf("That number belongs to %s.\n\n", tbp->entry[pos].name); }
    
    int insertNew(TelBook *tbp, int n, int *ip)
    {  char c, newname[81], newnum[81];
      int j=0,k;
      printf("Enter name: ");
      c=getchar();
      do { c=toupper(c);
         newname[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       newname[j]='\0';
      for (k=0;k<=j-2;k++) newname[k]=newname[k+1];
      newname[k]='\0';
    
      printf("Enter Number: ");
      j=0;
      c=getc(stdin);
      do { c=toupper(c);
         newnum[j]=c;
         j++;
         c=getc(stdin); } while (c!='\n');
       newnum[j]='\0';
    
      if (binarySearch(tbp, ip, newname,n))
         { strcpy(tbp->entry[*ip].telnum,newnum);
           printf("Old Entry Modified\n"); }
      else { for (j=n; j>=*ip; j--)
               { strcpy(tbp->entry[j].name, tbp-> entry[j-1].name);
              strcpy(tbp->entry[j].telnum, tbp-> entry[j-1].telnum); }
           free(tbp->entry[j].name);
           free(tbp->entry[j].telnum);
           strcpy(tbp-> entry[j].name,newname);
           strcpy(tbp-> entry[j].telnum,newnum);
           n++;
           printf("New entry inserted.\n"); }
    
      return n; }
    
    int deleteEntry(TelBook *tbp, int n)
    {  char name[81], c;
      int i, *ip=&i, j=0, k;
    
      printf("Enter Name: ");
      c=getchar();
      do { c=toupper(c);
         name[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       name[j]='\0';
      for (k=0;k<=j-2;k++) name[k]=name[k+1];
      name[k]='\0';
      if (binarySearch(tbp, ip, name, n))
         { free(tbp->entry[i].name);
           free(tbp->entry[i].telnum);
           for (j=i; j<=n-2; j++)
                {  strcpy(tbp->entry[i].name,tbp->entry[i+1].name);
                   strcpy(tbp->entry[i].telnum,tbp->entry[i+1].telnum);}
           printf("Entry deleted. \n");
           n--;
         }
      else printf("Name not found.\n");
    return n;}
    
    
    void listNames(TelBook *tbp, int n)
    { char c, name[MAX_STRING_SIZE];
      int j=0, k;
    
      printf("Enter first part of name: ");
      c=getchar();
      do { c=toupper(c);
         name[j]=c;
         j++;
         c=getchar(); } while (c!='\n');
       name[j]='\0';
      for (k=0;k<=j-1;k++) name[k]=name[k+1];
      name[k]='\0';
      printf("\n");
      for (k=0;k<=n-1;k++)
         if (tbp->entry[k].name==strstr(tbp->entry[k].name,name))
              printf("%s: %s\n", tbp->entry[k].name, tbp->entry[k].telnum);
      printf("\n");
    }
    
    void outputData(TelBook *tbp, int n)
    { 
     FILE *fp;
     int j;
    
      if ((fp=fopen("tbfile.txt", "w"))==NULL)
         { printf("The \"tbfile.txt\" could not be opened.\n");
           exit(1); }
      for (j=0;j<=n-1; j++)
         {  fputs(tbp->entry[j].name,fp);
          fputc('\n',fp);
          fputs(tbp->entry[j].telnum,fp);
          fputc('\n',fp); }
      fclose(fp);
    
      }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's my version of your insert function. It's different to yours, see how you get on with it. I haven't compiled it, and can't guarantee it will work, but it should give you an idea of how to do things.

    Also, using strcpy() all over the place in your program is going to get you into trouble. For example, say you have a pointer that you assigned a malloc'd sector of memory to, length of 10 bytes to hold a 9 byte name. You then go and copy a new name into that same memory area, but the name is 20 bytes long. Your compiler won't complain, but your program may or may not crash at some point. As you are using pointers, you need to work in a manner I have shown in my code below.

    Code:
    int insertNew(TelBook *tbp, int n, int *ip)
    {
      char *newname, *newnum;
      int j = -1;
      /*
       * read the two lines of input from the user
       */ 
      printf("Enter name: ");
      fflush(stdout);
      newname = fgetString(stdin);
      
      printf("Enter telnum: ");
      fflush(stdout);
      newnum = fgetString(stdin);
      
      if (newname != NULL && newnum != NULL)
      {
        strToUpper(name);
        if (binarySearch(tbp, ip, newname, n))
        {
          /* Update the telnum. Don't use strcpy, just free
           * the previous entry, and update the pointer
           */
          free(tbp->entry[*ip].telnum);
          tbp->entry[*ip].telnum = newnum;
          printf("Old Entry Modified\n");
          j = *ip;  /* For return purposes, you may choose otherwise */
        }
        else
        {
          /*
           * Loop through the array from the end backwards,
           * moving entries that are greater than the new name
           */
          for (j = n-1; j > 0; j--)
          {
            if (strcmp(tbp->entry[j].name, newname) > 0)
            {
              tbp->entry[j+1].name   = tbp->entry[j].name;
              tbp->entry[j+1].telnum = tbp->entry[j].telnum;
            }
            else break; /* Leave the loop early when we don't need to
                           do any more moves */
          }
          /*
           * Now j is set to where we want to insert the new item
           */
          tbp->entry[j].name   = newname;
          tbp->entry[j].telnum = newnum;
        }
      }
      
      return j; /* This could be -1 if the input failed */
    }
    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. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM

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