Question

****Using C and only C****

I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below:

Enter the Record Details: Enter Account number:909 Enter The Name: jo pi Enter The Address ...Program finished with exit code o Press ENTER to exit console.

Here is my code:

#include<stdio.h>
#include<stdlib.h>
struct record
{
int accountno;
char name[25];
char address[80];
struct record* next;
};

void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList
{
struct record *current,*start,*temp; //DeclareStructure Record Variables,
//Allocating Dynamic Memory Location
if(newRecord == NULL)
{
printf("Failed to Allocate Memory");
}
else
{
printf("Enter the Record Details: \n"); //Reading The Record Details
printf("Enter Account number:");
scanf("%d",&newRecord->accountno);
printf("Enter The Name:");
scanf("%s",&newRecord->name);
printf("Enter The Address");
scanf("%s",&newRecord->address);
  
newRecord->next=NULL; //Pointing to Next Record to NULL

if(start==NULL)
{
start=newRecord; //Pointing Starting Record if Starting record is NULL
current=newRecord; //Current Record Becomes First Record
}
else
{
temp = start;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = newRecord;
}
}
}
int main()
{
struct record * test = (struct record *)malloc(sizeof(struct record));
addRecord(test);
  
return 0;
}

PLEASE FIX what is going wrong in my code. Your help is appreciated.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <stdio.h>

#include <stdlib.h>

struct record

{

int accountno;

char name[25];

char address[80];

struct record *next;

};

struct record *start = NULL;

void addRecord(struct record *newRecord) //Function For Adding Record at last in a SinglyLinkedList

{

struct record *current, *temp; //DeclareStructure Record Variables,

//Allocating Dynamic Memory Location

if (newRecord == NULL)

{

printf("Failed to Allocate Memory");

}

else

{

printf("Enter the Record Details: \n"); //Reading The Record Details

printf("Enter Account number:");

scanf("%d", &newRecord->accountno);

printf("Enter The Name:");

scanf("\n%[^\n]s", newRecord->name);

printf("Enter The Address: ");

scanf("\n%[^\n]s", newRecord->address);

newRecord->next = NULL; //Pointing to Next Record to NULL

if (start == NULL)

{

start = newRecord; //Pointing Starting Record if Starting record is NULL

current = newRecord; //Current Record Becomes First Record

}

else

{

temp = start;

while (temp->next != NULL)

{

temp = temp->next;

}

temp->next = newRecord;

}

}

}

int main()

{

struct record *test = (struct record *)malloc(sizeof(struct record));

addRecord(test);

printf("\n\nAccount No: %d\nName: %s\nAddress: %s\n", test->accountno, test->name, test->address);

return 0;

}

nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/29092018$ gcc accnt.c nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/September/29092018$ ./a.out Enter the Record Details Enter Account number:909 Enter The Name:anunaga Enter The Address: berkely street, usa Account No: 909 Name: anunaga Address: berkely street, usa

Add a comment
Know the answer?
Add Answer to:
****Using C and only C**** I have some C code that has the function addRecord, to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Please follow the information here, explain your answer, and show that your code works. Your help...

    Please follow the information here, explain your answer, and show that your code works. Your help is much appreciated. Write the pseudocode of the a function, deleteRecord, to delete a record in the linked list of records. Assume the following variables are already defined, initialized and available for you to use: start: The pointer to the first record of the list (or NULL) uaccountno: The user's account number (integer) Assume the following struct definition: struct record int char char struct...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Q 1. Q 2. if it works, I'll thumb up for u. Complete the function createStudent...

    Q 1. Q 2. if it works, I'll thumb up for u. Complete the function createStudent below: #include <stdio.h> #include <stdlib.h> struct student { char name[50]; int age; struct student *next; }; struct student * createStudent(char studentName[], int studentAge) { } int main() { struct student *studptr; int myAge; char myName[50]; scanf("%s %d", myName, &myAge); studptr = createStudent(myName, myAge); printf("Student %s is %d years old.\n", studptr->name, studptr->age); free(studptr); return 0; } For example: Test Input Result Zhangwei Student Zhangwei is...

  • In C , checks if the entered password for admin is correct by calling checkAdminPassword. Part...

    In C , checks if the entered password for admin is correct by calling checkAdminPassword. Part of this function has been implemented, complete the statement in if(). Keep in mind that the entered password is plaintext whereas the password in struct user is in the hashsed form. The password for user admin is “s#1Pa5”. int checkAdminPassword(char* password, struct user* users, int count) { for (int i = 0; i < count; ++i) { if (strcmp((users + i)->username, "admin") == 0)...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT