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!
Looks like your hosing the input when you enter bad input for an integer. Try making sure the user gave correct inputting by using
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.
if( std::cin.fail( ) ) // Bad Input
std::cin.clear( ) // Clear error bits
// Do Something...
std::cin.ignore( 80, '\n' ); // ignore excess characters
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!
I'm not sure I understand the std::cin.ignore() function, but what you showed me did work, thanks (:
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
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.