Thread: Linked Lists

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    Linked Lists

    Hi, I just made a program that manipulated linked lists, and I'm quite new to them. What it does is use a basic console input and you enter the number for the option you want. But if I enter a the wrong number, it says Incorect input! Please Wait . . . then it waits for a few to inform the user. But if I press a letter, it wont go back to the menu.

    Also, on the linked lists part, I'm not sure if I should use the delete command, I have no idea.

    it was written and compiled in Visual C++

    I'd appreciate any help/advice, thanks!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Looks like your hosing the input when you enter bad input for an integer. Try making sure the user gave correct inputting by using

    Code:
    if( std::cin.fail( ) ) // Bad Input 
    {
      std::cin.clear( ) // Clear error bits
      // Do Something...
      std::cin.ignore( 80, '\n' ); // ignore excess characters
    }
    You can use something like that to help the bad input. Basically you if you enter something like "aasdaf" when its expecting an integer, next time it loops around something will already be in the buffer so it'll skip over the cin statement.

    As far as your concern about delete goes you should be using that to free your linked list when you are finished. Go through your list carefully with the use of temp pointers and free each node that you allocated until you reach the end.

    Also, your logic may be a little off. Try doing this with the debugger. First display your empty list. Then try to add something to it. You will try to dereference a null pointer. Bad Times!
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    27
    I'm not sure I understand the std::cin.ignore() function, but what you showed me did work, thanks (:
    Last edited by Anglos; 08-21-2003 at 11:32 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    std::cin.ignore( 80, '\n' );

    the above contains the following information, in order:

    scope/scope resolution operator/object name/dot operator/method name/number of char to ignore/terminating char

    and means:

    use the istream method called ignore() of the object called cin found in scope namespace std to ignore either the next 80 char in the input stream buffer or all char until a newline char is found in the input stream buffer, whichever comes first. If newline char is found, remove it from the buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM