Linked Lists aaaugggh

This is a discussion on Linked Lists aaaugggh within the C++ Programming forums, part of the General Programming Boards category; Okay I think I have the main idea of whats goin on here, but when I run it there are ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Linked Lists aaaugggh

    Okay I think I have the main idea of whats goin on here, but when I run it there are multiple bugs. Note: this is not my code, I just want to discuss it.

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    struct node
      {  char name[20];    // Name of up to 20 letters
         int age;          // D.O.B. would be better
         float height;     // In metres
         node *nxt;// Pointer to next node
      };
    
    node *start_ptr = NULL;
    node *current;		 // Used to move along the list
    int option = 0;
    
    void add_node_at_end()
      {  node *temp, *temp2;   // Temporary pointers
    
         // Reserve space for new node and fill it with data
         temp = new node;
    	 cout << "Please enter the name of the person: ";
         cin >> temp->name;
         cout << "Please enter the age of the person : ";
         cin >> temp->age;
         cout << "Please enter the height of the person : ";
         cin >> temp->height;
         temp->nxt = NULL;
    
         // Set up link to this node
         if (start_ptr == NULL)
           { start_ptr = temp;
    	 current = start_ptr;
           }
         else
           { temp2 = start_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    
    void display_list()
      {  node *temp;
         temp = start_ptr;
         cout << endl;
         if (temp == NULL)
           cout << "The list is empty!" << endl;
         else
           { while (temp != NULL)
    	   {  // Display details for what temp points to
                  cout << "Name : " << temp->name << " ";
                  cout << "Age : " << temp->age << " ";
    	      cout << "Height : " << temp->height;
    	      if (temp == current)
    		cout << " <-- Current node";
                  cout << endl;
    	      temp = temp->nxt;
    
    	   }
    	 cout << "End of list!" << endl;
           }
      }
    
    void delete_start_node()
       { node *temp;
         temp = start_ptr;
         start_ptr = start_ptr->nxt;
         delete temp;
       }
    
    void delete_end_node()
       { node *temp1, *temp2;
         if (start_ptr == NULL)
              cout << "The list is empty!" << endl;
         else
            { temp1 = start_ptr;
              if (temp1->nxt == NULL)
                 { delete temp1;
                   start_ptr = NULL;
                 }
              else
                 { while (temp1->nxt != NULL)
                    { temp2 = temp1;
                      temp1 = temp1->nxt;
                    }
                   delete temp1;
                   temp2->nxt = NULL;
                 }
            }
       }
    
    void move_current_on ()
       { if (current->nxt == NULL)
          cout << "You are at the end of the list." << endl;
         else
          current = current->nxt;
       }
    
    void move_current_back ()
       { if (current == start_ptr)
          cout << "You are at the start of the list" << endl;
         else
          { node *previous;     // Declare the pointer
            previous = start_ptr;
    
            while (previous->nxt != current)
              { previous = previous->nxt;
              }
            current = previous;
          }
       }
    Something is wrong with this code, I'm trying to debug it but I just can't figure out the problem. It compiles fine, but the problem happens when I enter a person's name with a space in it. It seems to run fine until I type in Bob Marley instead of BobMarley.

    Then instead of running in a procedural manner it seems to just output all the remaining calls of add_node_at_end(); and skip the cin's.

    Here is the main function
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	srand ( unsigned(time(NULL)) );
    	start_ptr = NULL;
    	add_node_at_end();
    	add_node_at_end();
    	add_node_at_end();
    	add_node_at_end();
        return 0;
    }
    The first two add calls I keep everything kosher and without any spaces
    The third call though I add one space, and look it fudges everythign up :d
    Code:
    Please enter the name of the person: bobmarley
    Please enter the age of the person : 40
    Please enter the height of the person : 100
    Please enter the name of the person: shamino55
    Please enter the age of the person : 20
    Please enter the height of the person : 100
    Please enter the name of the person: hereisaspace !
    Please enter the age of the person : Please enter the height of the person : Please enter the name o
    f the person: Please enter the age of the person : Please enter the height of the person : Press any
     key to continue . . .
    Any ideas guys?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    cout << "Please enter the name of the person: ";
    cin >> temp->name;
    cout << "Please enter the age of the person : ";
    cin >> temp->age;
    This has less to do with linked lists and more with cin operator >> reading input only up to whitespace, leaving the rest to be unread.

    If you input "spacehere !", temp->name gets "spacehere" and then cin tries to put the remaining "!" into temp->age. Since that is an integer, the operation fails, cin goes into a bad state and from here on all inputs fail.

    To get a line of input use getline.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    But beware of previous >> operations leaving newlines in the stream.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Um... Okay I've never used getline()

    Oh no!

    Um, can anyone give me a reason as to why this doesn't work?

    Code:
    void add_node_at_end()
      {  node *temp, *temp2;   // Temporary pointers
    
         // Reserve space for new node and fill it with data
         temp = new node;
    	 cout << "Please enter the name of the person: ";
    	 getline(temp->name, 256);
         cout << "Please enter the age of the person : ";
         getline(temp->age, 256);
         cout << "Please enter the height of the person : ";
         getline(temp->height,256);
         temp->nxt = NULL;
    
         // Set up link to this node
         if (start_ptr == NULL)
           { start_ptr = temp;
    	 current = start_ptr;
           }
         else
           { temp2 = start_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    Here are the errors
    Code:
    ------ Build started: Project: Blackjack, Configuration: Debug Win32 ------
    Compiling...
    Card_Game.cpp
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\linked_list.h(22) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'char [256]'
            c:\program files\microsoft visual studio 8\vc\include\string(528) : see declaration of 'std::getline'
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\linked_list.h(22) : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
            c:\program files\microsoft visual studio 8\vc\include\string(476) : see declaration of 'std::getline'
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\linked_list.h(24) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
            c:\program files\microsoft visual studio 8\vc\include\string(528) : see declaration of 'std::getline'
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\linked_list.h(24) : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
            c:\program files\microsoft visual studio 8\vc\include\string(476) : see declaration of 'std::getline'
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\linked_list.h(26) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'float'
            c:\program files\microsoft visual studio 8\vc\include\string(528) : see declaration of 'std::getline'
    c:\documents and settings\jcoleman\desktop\blackjack\blackjack\linked_list.h(26) : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
            c:\program files\microsoft visual studio 8\vc\include\string(476) : see declaration of 'std::getline'
    Build log was saved at "file://c:\Documents and Settings\jcoleman\Desktop\Blackjack\Blackjack\Debug\BuildLog.htm"
    Blackjack - 6 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That version of getline is a member function of istream.
    Try
    Code:
    cin.getline(temp->name, 256);
    Kurt

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Okay that fixed most of the errors, but now it's telling me I can't give getline an integer, can I do a type cast of int to char?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Shamino View Post
    Okay that fixed most of the errors, but now it's telling me I can't give getline an integer, can I do a type cast of int to char?
    I see. you're talking about that one
    Code:
         getline(temp->age, 256);
    Doesn't work. getline reads only strings.
    Kurt
    EDIT: use a stringstream to convert to int
    Last edited by ZuK; 12-27-2007 at 10:09 AM.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    So basically, use a string for now, but when I read it out convert it to an int?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,006
    Yes, something like that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Rather, whenever I'm trying to do arithmetic with a number then convert it to an integer.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,006
    To sum it up short, whenever you need your number to be of type int. When you need to process it as an integer. It's perfectly fine to display it as a string to the screen - that doesn't matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Cool beans.

    Okay, lets say I dont like his linked list, and I want a linked list.h and namespace in there.

    I want the node to be a template, with a pointer to data of type T_

    Is that feasable?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Why do you want to getline() the age anyway? Surely it's just going to be a number, right? If you read in "3 1" it would be an invalid entry, correct? So just read in what you can. Screw the getline() in that case and cin>> it.

    >> Is that feasable?
    Yes. Have a go at it and come back if you have problems.
    You might as well make a class out of it, by the way.

  14. #14
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    a class? out of node? if i make it a template? you're probably right.

    But if I have a namespace shoudl the functions that control the nodes be int he namespace or members of the class?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Members of the class.
    You familiar with std::vector? Try to model it on that.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

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