Note that there are some explanatory texts on larger screens.

plurals
  1. POStructs and malloc code getting weird outputs
    primarykey
    data
    text
    <p>Its me again and making progress... I want to say thanks to everyone who commented on my last question, it was very helpful. I got this to compile so far but having some weird bugs that I can't iron out. </p> <pre><code>void addRecord(){//carries users input data numRecs++;//increments numRecs by 1 struct record library; //this will hold info for user input printf ("Please enter your first name:\n"); fgets(library.fName, sizeof(library.fName), stdin); printf ("Please enter your last name:\n"); fgets(library.lName, sizeof(library.lName), stdin); printf ("Please enter your hometown:\n"); fgets(library.hometown, sizeof(library.hometown), stdin); 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); struct record *myNewRecord;//creates a new struct pointer to store all the old data and new data myNewRecord = malloc(numRecs * sizeof(struct record)); //allocates space to fit all old data plus the new struct data if (myNewRecord == NULL) { fprintf(stderr,"Out of memory\n"); } *myNewRecord = library; fprintf(stderr, "You made it here!!\n"); </code></pre> <p>These are the results I get from the terminal.. It looks like the syntax is all correct in the source code but the issue is it skips the first name fgets for some reason. Also, when it prints out it executes a return somehow. Can you guys see whats going on??? P.S. It doesn't do this when I eliminate the switch case and only have addrecord() in the main.</p> <pre><code>ubuntu@ubuntu:~$ ubuntu@ubuntu:~$ gcc lab222.c -o lab222 ubuntu@ubuntu:~$ ./lab222 Please select from the following: 1. Print all records. 2. Print number of records. 3. Print size of database. 4. Add record. 5. Delete record. 6. Print number of accesses to database. 7. Exit. Enter a number 1-7:4 Please enter your first name: Please enter your last name: Don Please enter your hometown: Mega You entered for your first name. You entered Don for your last name. You entered Mega for your hometown. You made it here!! </code></pre>
    singulars
    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.
 

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