Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A few observations,</p> <ul> <li>Save reallocating the array and copying the previous values for every record</li> <li>allocate an array of pointers to new records, and fill that array as you add records</li> <li>you can reallocate that pointer array much cheaper than the data records</li> <li>move you menu display and menu choice determination to separate functions</li> <li>you will probably add a linked list (head, mover?), so the pointer array is close</li> <li>use fgets to read input, it is less twitchy than scanf</li> <li>you can still use sscanf to extract your menu choice (that fixed the problem)</li> </ul> <p>Here is the revised record storage,</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; //needed for malloc, free #include&lt;string.h&gt; // needed for the memcpy #define MAX 100 struct record { char fName[MAX]; char lName[MAX]; char hometown[MAX]; }; //structure template/declaration struct record* records[500]; //allow room for 500 (for now) int numRecords=0; //stores number of records, used for several tasks //prototypes int addRecord(); </code></pre> <p>here are functions to display menu and read input (view)</p> <pre><code>//********MENU*********** int menushow(FILE* fh) { printf("\nPlease select from the following:\n"); printf(" 1. Print all records.\n"); printf(" 2. Print number of records.\n"); printf(" 3. Print size of database.\n"); printf(" 4. Add record.\n"); printf(" 5. Delete record.\n"); printf(" 6. Print number of accesses to database.\n"); printf(" 7. Exit.\n"); printf("Enter a number 1-7:"); return 0; } int menudo(char* line) { int sel; int choice; if( sscanf(line,"%d",&amp;sel) &lt; 1 ) { fprintf(stderr, "Error, not a valid input. Must be a number from 1 to 7.\n"); return 0; } switch(choice = atoi(line)) { case 1: break; case 2: break; case 3: break; case 4: if( addRecord() &lt; 0 ) //This creates a new record based of input into struct library. fprintf(stderr,"addRecord failure\n"); break; case 5: break; case 6: break; case 7: exit(0); break; default: printf("\nError, not valid input. Please enter a number from 1 to 7.\n\n"); break; } return choice; } </code></pre> <p>Here is main with the menu processing extracted to functions,</p> <pre><code>#define EXITCHOICE 7 int main(void) { char line[MAX+1]; int action=0; while (action&lt;EXITCHOICE) { menushow(stdout); if( !fgets(line,sizeof(line),stdin) ) { break; } //printf("entered: %s\n",line); if( (action=menudo(line)) &lt; 0 ) break; } return 0; } </code></pre> <p>and here is the simpler addrecord,</p> <pre><code>//** Beginning of addRecord** //returns -1 on failure int addRecord() {//carries users input data struct record library; //this will hold info for user input struct record *myNewRecord; //creates a new struct pointer to store all the old data and new data printf ("Please enter your first name:\n"); if(!fgets(library.fName, sizeof(library.fName), stdin)) return -1; printf ("Please enter your last name:\n"); if(!fgets(library.lName, sizeof(library.lName), stdin)) return -1; printf ("Please enter your hometown:\n"); if(!fgets(library.hometown, sizeof(library.hometown), stdin)) return -1; printf("You entered %s for your first name.\n", library.fName); printf("You entered %s for your last name.\n", library.lName); printf("You entered %s for your hometown.\n", library.hometown); records[numRecords] = (struct record*)malloc(sizeof(struct record)); //allocates space to fit all old data plus the new struct data if (records[numRecords] == NULL) { fprintf(stderr,"error: Out of memory\n"); return -1; } memcpy(records[numRecords],&amp;library,sizeof(struct record)); numRecords++;//increments numRecords by 1 fprintf(stderr, "[%d] records!!\n",numRecords); return(numRecords); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload