incorect value of node

This is a discussion on incorect value of node within the C++ Programming forums, part of the General Programming Boards category; I have a simple function that adds a node to a linked list: Code: node *x = new node; cout ...

  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-19-2008 at 11:26 PM.

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

    I suppose you want it another way
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    x->value = val[0];

    as with anything else what you already successfully set
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03: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, 09: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

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