Thread: incorect value of node

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    incorect value of node

    I have a simple function that adds a node to a linked list:
    Code:
          node *x = new node;  
          cout << "enter value" << endl;
          cin >> x -> value;
          x -> next = head;
          head = x;
    This works fine, I get output in this form:
    node: a
    node: b
    node: c
    node: d
    node: e <- I entered this value via the function

    but when I pass the value to the function via string val

    Code:
          node *x = new node;  
          cout << "val[0] = " << val[0] << endl;    // prints e
          val[0] = x -> value; 
          x -> next = head;
          head = x;
    output is like
    node: a
    node: b
    node: c
    node: d
    node: 8 <- should be e
    What is wrong?

    PS. value (in node) is of type char.
    Last edited by Suchy; 02-20-2008 at 12:26 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    val[0] = x -> value;
    you assign uninitialized value x -> value to val[0]

    I suppose you want it another way
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    what I want is the set value to what ever val[0] is, how can I do this?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    x->value = val[0];

    as with anything else what you already successfully set
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM