Yong Hao Yong Hao - 8 months ago 80
C Question

Run-Time Check Failure #3 - The variable 'name_t' is being used without being initialized

#include <stdio.h>
#include <stdlib.h>

This is the structure i declared

Just right after entered the student number and the moment i hit enter key and it give me the error "Run-Time Check Failure #3 - The variable 'name_t' is being used without being initialized."

struct node
int data;
int number;
char name, transaction;
struct node *next;

}*newNode, *list, *temp;

void insertNode();
void displayNode();
void deleteNode();

Below is the insert function i wrote

void insertNode()
int number_t;
char name_t, transaction_t;

newNode = (struct node*) malloc(sizeof(struct node));

printf("Enter Student number: ");
scanf("%d", &number_t);

newNode->number = number_t;
newNode->next = NULL;

printf("Enter Student name: ");
scanf("%s", name_t);

newNode->name = name_t;
newNode->next = NULL;

printf("Enter Transcation Desciption: ");
scanf("%s", transaction_t);

newNode->transaction = transaction_t;
newNode->next = NULL;

if (list == NULL)
list = newNode;
temp = list;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;

scanf("%s", name_t);

scanf is expecting a string, but you are passing a plain char (with not enough room to store a string)

I suggest fgets in order to avoid buffer overflows:

char name[100]; /* name_t is not a good name for a variable */
char *ptr;

if (fgets(name, sizeof name, stdin)) {
    /* strip the trailing newline */
    ptr = strchr(name, '\n');
    if (ptr != NULL) {
        *ptr = '\0';