Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the code below but am not able to get the intended output.

//Program Start
Option = 1
Name = Sample

//Output
NULL

What I have tried:

C++
struct node{
    char *name;
    struct node *link;
};

struct node *head, *temp, *ptr;

void getNameStart(){
    temp = (struct node *) malloc (sizeof(struct node));

    temp -> name = DynamicName();
    temp -> link = head;
    head = temp;
}

void Print(){
    temp = head;
    while(temp != 0){
        printf("\n%s", temp -> name);
        temp = temp -> link;
    }
}

void DynamicName(){
    char *name, c;
    int i=0, j=2;

    fflush(stdin);
    printf("\nEnter name : ");
    c = getchar();
    name = (char *) malloc (sizeof(char));

    while(c != '\n'){
        name[i] = c;
        name = (char *) realloc (name, sizeof(char) * j);
        c = getchar();
        i++, j++;
    }
    name[i] = '\0';
    return name;
}

int main(){
    head = 0;
    int i=0, op1;
    char op[5];

    do{
        printf("\nSelect option : ");
        printf("\n\n1. Insert at beginning\n2. Insert at end\n3. Insert anywhere");
        printf("\n\nYour choice : ");
        scanf("%d", &op1);

        switch (op1){
            case 1 : getNameStart(); break;
        }

        printf("\nInsert more nodes? : ");
        fflush(stdin);
        gets(op);

    }   while(strcmp(op, "YES")==0 || strcmp(op, "yes")==0);

    printf("\n\nThe list is : ");
    Print();
}
Posted
Updated 3-Mar-17 20:01pm
v2
Comments
Garth J Lancaster 4-Mar-17 1:14am    
have you stepped through your program with a debugger and inspected the contents of your variables ? if you cant becuase you're not working in an environment that has a debugger, you can use printf() statements - for example :-

temp -> name = DynamicName();
temp -> link = head;

insert a printf to check name is being returned from your DynamicName() function - if you're not getting anything back from DynamicName, thats where you start looking

temp -> name = DynamicName();
printf("name = [%s]\n", temp => name);
temp -> link = head;

I'm not sure I like your DynamicName function or if it even works - you havnt said what environment you're using, you could use getline() instead of re-inventing the wheel
Member 13037163 4-Mar-17 1:26am    
I figured out the problem, I was using a void function which was trying to return a value.
Thank you so much for the help.

1 solution

Quote:
I figured out the problem,

Even if you have found what was the problem, The advice of learning to use a debugger is something to consider.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 13037163 4-Mar-17 2:05am    
Thank you, Ill keep it in mind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900