Random structure selection problem

This is a discussion on Random structure selection problem within the C Programming forums, part of the General Programming Boards category; Hi everyone! I work on my phone book program. The only problem there is random structure selection function. I've tried ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    1

    Random structure selection problem

    Hi everyone! I work on my phone book program. The only problem there is random structure selection function. I've tried to assign value of "int counter". The program compiles, but doesn't run at all. I also tried to set "ilName = rand () % counter;" to ilName = rand () % 50;" which actually doesn't have sense if the phone book is not full. But the whole program executes at least in this case. The question is about "case 6" specifically, but I post the whole code just in case. I got stuck and don't see even simple mistakes! Somebody, please help! Here is what I've done so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <time.h>
    
     
    typedef struct pbEntry
    
    {
         char fName[25];
         char lName[25];
         char pNumber[25];
         
    } person;
    
    void addFriend (int*, int, person*);  // Add function prototype
    void deleteFriend (int*, int, person*);  // Delete function prototype
    void printBook (int*, int, person*);    // Print function prototype
    int compare (const void *a, const void *b); // Compare function prototype
    void findFriend (int*, int, person*); // Find function prototype
    void deleteAll (int*, int, person*);  // Delete function prototype
    
     
    main()
    
    {
          
          char lName[25], fName[25], pNumber[25];
          
          int iResponse = 0;
          int i;
          int counter;
       
          person contacts[50];
          person* pContacts;
          pContacts = (person*) calloc(0, sizeof(person));
          
          srand (time (NULL));
          int ilName;
          ilName = rand () % counter;
    
                
    do {
        
        printf ("\n\t\t\tPhone Book\n");
        printf ("\n\t1)\tAdd friend\n");
        printf ("\n\t2)\tDelete friend\n");
        printf ("\n\t3)\tShow phone book\n");
        printf ("\n\t4)\tSort entries alphabetically\n");
        printf ("\n\t5)\tFind friend\n");
        printf ("\n\t6)\tRandom friend\n");
        printf ("\n\t7)\tDelete all entries\n");
        printf ("\n\t8)\tExit\n");
        
        printf ("\n\n\tPlease, enter your choice (1 - 8): \n");
        scanf ("%d", &iResponse);
        
              switch (iResponse) {
                     
                     case 1:
                          
                          pContacts = realloc (pContacts, counter * sizeof (person));
                          addFriend (&counter, i, contacts);
                          
                          break;
                        
                     case 2:
                        
                          deleteFriend (&counter, i, contacts);
                        
                          break;
                        
                     case 3:
                        
                          printBook (&counter, i, contacts);
                        
                          break;
                        
                     case 4:
                          
                          qsort (contacts, counter, sizeof (person), compare);
                        
                          for(i = 0; i < counter; ++i) {
                              
                              printf("\n\t%s %s %s\n", contacts[i].lName, contacts[i].fName, contacts[i].pNumber);
                              
                              } //end for loop
                                           
                          break;
                          
                     case 5:
                          
                          findFriend (&counter, i, contacts);
                          
                          break;
                     
                     case 6:
                          
                          printf("\n\t%s %s %s\n", contacts[ilName].lName, contacts[ilName].fName, contacts[ilName].pNumber);
                          
                          break;
                     
                     
                     case 7:
                        
                          deleteAll (&counter, i, contacts);
                        
                          break;
                             
                     case 8:
                        
                          system ("CLS");
                          printf ("\n\n\t\tGood bye!\n");
                
                          getch ();
                          break;
                        
              default:
                      
                      printf ("\n\tInvalid choice.\n");
                        
                      getch ();                    
                      break;
                        
              }  // end switch
                
        }  // end loop
             
    while (iResponse != 8);
        
    } // end main
     
     
    /*Add entry function*/
    
    void addFriend (int *counter, int i, person *contacts) // function defenition
    
    {
         (*counter)++;
         
         printf ("\n\tPlease, enter the last name: \n");
         scanf("%s", contacts [*counter - 1].lName);
         printf ("\n\tPlease, enter the first name: \n");
         scanf("%s", contacts [*counter - 1].fName);
         printf ("\n\tPlease, enter the phone number: \n");
         scanf("%s", contacts [*counter - 1].pNumber);
         
         printf("\n\t%s %s was added to the phone book.\n\n", contacts [*counter - 1].lName, contacts [*counter - 1].fName);
        
    } // end function
    
    
             /*Delete entry function*/
    
    void deleteFriend (int *counter, int i, person *contacts) // function definition
    
    {
         
         char lNameDel[25];
         char fNameDel[25];
         char nullString[25] = {"\0"};
         
         printf ("\n\tPlease, enter the last name: \n");
         scanf("%s", lNameDel);
         printf ("\n\tPlease, enter the first name: \n");
         scanf("%s", fNameDel);
         
         for (i = 0; i < *counter; i++)
             
             if (strcmp(lNameDel, contacts[i].lName) == 0) break;
                                  
             if (i != *counter) {
                                  
                                  strcpy(contacts[i].lName, nullString);
                                  strcpy(contacts[i].fName, nullString);
                                  strcpy(contacts[i].pNumber, nullString);
    
                                  printf("\n\t%s %s record has been deleted from the phone book.\n", lNameDel, fNameDel);
                                  
                                  } // end if
             
             else
             
                   printf ("\n\t%s %s record does not exist in the Phone Book.\n", lNameDel, fNameDel);
                    
             getch ();                    
                 
    } // end function
    
    
             /*Print Book function*/
    
    void printBook (int *counter, int i, person *contacts) // function definition
    
    {
         
         char nullString [25] = {"\0"};
        
         if (*counter > 0) {
                     
                     printf("\n\tPhone Book entries:\n\n");
                     
         for(i = 0; i<*counter; i++) {
                 
                 if (strcmp(nullString, contacts[i].fName) != 0) {
                                        
                                        printf("%s %s %s\n", contacts[i].lName, contacts[i].fName, contacts[i].pNumber);
                                        
                                        } // end if
                                        
                 } // end for loop
                 
        } // end if
        
        else {
             
             printf("\n\tThe phone book is empty.\n\n");
             
        } // end else
        
    } // end function
    
    
             /*Compare function for qsort*/
             
    int compare (const void *a, const void *b) // function definition
         
    {
         person *ia = (person *)a;
         person *ib = (person *)b;
         return strcmp (ia -> lName, ib -> lName);
         
    } // end function
    
    
             /*Find entry function*/
    
    void findFriend (int *counter, int i, person *contacts) // function definition
    
    {
         
         char lName[25];
         char fName[25];
         
         printf ("\n\tPlease, enter the last name: \n");
         scanf("%s", lName);
         printf ("\n\tPlease, enter the first name: \n");
         scanf("%s", fName);
         
         for (i = 0; i < *counter; i++)
             
             if (strcmp(lName, contacts[i].lName) == 0 && strcmp(fName, contacts[i].fName) == 0) break;
             
             if (i != *counter)
                
                   printf ("\n\t%s %s phone number is %s\n", lName, fName, contacts[i].pNumber);
                   
             else
             
                   printf ("\n\t%s %s record does not exist in the Phone Book.\n", lName, fName);
                 
             getch ();                    
                 
    } // end function
    
    
    
             /*Delete all function*/
    
    void deleteAll (int *counter, int i, person *contacts) // function definition
    
    {
         
         char nullString[25] = {"\0"};
         
         for (i = 0; i < *counter; i++) {
             
             if (strlen(contacts[i].lName) != 0) {
                                  
                                  strcpy(contacts[i].lName, nullString);
                                  strcpy(contacts[i].fName, nullString);
                                  strcpy(contacts[i].pNumber, nullString);
                                  
             } // end if
                                  
         } // end loop
     
        printf("\n\tThe Phone Book is empty.\n");
        
    } // end function

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Is there any chance that you had post the same question on DIC?

    Random Structure Selection Problem - C And C++ | Dream.In.Code

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    That statement executes only once before the main loop starts. What is the value of counter when that statement executes?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,690
    There's a lot here to comment on - but some biggies I see at a quick look-over:

    1 - You pass the address of "counter" to many functions, and derefence the pointer to get its value in those functions - why not just pass the variable itself and save two steps?

    2 - You start using your variables before you assign them values. In this case, the behavior of the program will be erratic and somewhat dangerous.

    For instance, you declare "counter," but it has no value when you use it to determine a random value:

    Code:
        int counter;
        // ...
        int ilName;
        ilName = rand () % counter;
    Ditto for much of your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Botched random choice selection function
    By Xom in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2011, 02:03 PM
  2. Need help with switch/case selection structure
    By nickadeemus2002 in forum C++ Programming
    Replies: 13
    Last Post: 06-29-2004, 03:16 PM
  3. Random selection of a word
    By doobyscoo in forum C Programming
    Replies: 1
    Last Post: 04-14-2003, 12:39 PM
  4. random number selection
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 09:02 AM
  5. random selection of words from a text file
    By archie in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 12:59 AM

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