Er, before anything else, you really need to watch your I/O. Don't mix C++ and C.
Find every
getch() (non-standard
evil!) and replace it with
cin.get();. Find every
gets(s); and replace it with
getline( cin, s );.
Also, I know everyone teaches and tutorials and etc. this, but try not to use
cin>>myint; when getting user input. Better to do what the user expects and head-off any input problems by getting a temporary string and converting it:
#include <sstream>
#include <iostream>
using namespace std;
int main() {
string s;
int i;
cout << "Please enter an integer> ";
getline( cin, s );
if (!(stringstream( s ) >> i))
cout << "That wasn't an integer!\n";
else
cout << "Thank you\n";
return EXIT_SUCCESS;
}
This code is very forgiving about input errors (gets a number whenever it can) and never leaves the input stream messed up.
OK, your linked list. You add a
first for the first, then you add an
x for every additional. However, you are still modifying
first, not
x in your input statements. Notice that when you display() the list the last one you entered is displayed properly, but all the others are garbage?
Also, you have memory leaks. What if the user chooses '1' twice? You should initialize
first to NULL before anything else in your program. Then check it at the case 1 to see if it is still NULL. If not, you need to either delete the whole list, or add, or give the user a choice, or something.
Well, this has gotten long and I haven't looked at delete yet.
Hope this helps.
[EDIT] Oh yeah, you should either use a
string in the student struct for the
name or verify that your user doesn't try to give you a name that is too long.
Never use gets().