Linked List Problem

This is a discussion on Linked List Problem within the C++ Programming forums, part of the General Programming Boards category; Hi, When I try to insert a name into my linked list I get output that looks something like: ==============&=== ...

  1. #1
    Registered User Daniel's Avatar
    Join Date
    Jan 2003
    Posts
    47

    Linked List Problem

    Hi,

    When I try to insert a name into my linked list I get output that looks something like: ==============&=== and I don't know why.

    Here is a simple version of my code which is causing my problems:

    Code:
    #include <iostream.h>
    
    struct node
    {
    	char name[20];
    	node *next;
    };
    
    int main()
    {
    	node * L= new node;
    	char newname[20];
    	cout<<"Enter a name:"<<endl;
    	cin >> newname;
    	L->name[20] = newname[20];
    	cout<<L->name;
    	return(0);
    }
    Any reasons why this is happening?

    Thanks in advance
    C++ Homepage: http://darkeldar77.tripod.com/cpp.html

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    strcpy owns you

    Code:
    #include <iostream.h>
    #include <string.h>
    
    struct node
    {
    	char name[20];
    	node *next;
    };
    
    int main()
    {
    	node * L= new node;
    	char newname[20];
    	cout<<"Enter a name:"<<endl;
    	cin >> newname;
    	strcpy (L->name, newname);
    	cout<<L->name;
    	return(0);
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    and when copying arrays, no need for [] operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21