By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Student List Using insert_at_end method

P: 26
hi everyone..im trying to create a student list program using linked list that will display all my info of students..but it seems theres a little prob. after i enter my first student the program will exit.. can somebody help me please. thank you


#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include <conio.h>

class student {

char name[30];
char gender[6];
int age;
student *next;
friend stud_list;

public:
student() {
cout << "Name: "; cin >> name; cout << endl;
cout << "Gender: "; cin >> gender; cout << endl;
cout << "Age: "; cin >> age; cout << endl;
next=NULL;
};
~student() {
cout << "Deleted!" << endl;
};
void display();
};

class stud_list {

int count;
student *first, *last;

public:

stud_list() {
count=0;
first=NULL; last=NULL;
};

~stud_list() {
cout << "List Deleted\n";
};


public:

void insert_at_end(student *a);
void insert_as_first(student *a);

};

void stud_list::insert_as_first(student *a) {

first=a;
last=first;
};

void student::display() {

cout << "Name: " << name << endl;
cout << "Gender: " << gender << endl;
cout << "Age: " << age << endl;

}

int main()
{
kdf
student a;
a.display();
getche();
return 0;
}
Dec 11 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
student() {
cout << "Name: "; cin >> name; cout << endl;
cout << "Gender: "; cin >> gender; cout << endl;
cout << "Age: "; cin >> age; cout << endl;
next=NULL;
};
This constructor is not correct. What I see here is your main(). The constructor is to initialize the object data members. You should have code like:
Expand|Select|Wrap|Line Numbers
  1. name[0] = '\0';
  2. gender[0] ] = '\0';
  3. age = 0;
  4. student::student() {
  5. next=NULL;
  6. };
  7.  
Then in main() you have:
Expand|Select|Wrap|Line Numbers
  1. student a;
  2. char buffer[80];
  3. cout << "Name: "; cin >> buffer; cout << endl;
  4. a.SetName(buffer);          //you need to write this method
  5.  
etc...

Also, there's no reason for stud_list to be a friend of stud. In general, friend classes are a no-no as they violate the laws of encapsulation. If stud_list needs to fiddle with the data of student, there should be a student method that can be called.

Also, this is C++ and not C. You should be using the string class and not the C string library. Your data members should be string objects and you should cin>> into a string buffer.

Also, your class methods are coded as inline when there is no reason to do so. The member function definitions should be outside the class declaration. There is a place for inline functions, and this problem is not the place.

Also, your linked list is twisted up with the student. Separate youe list from your data:
Expand|Select|Wrap|Line Numbers
  1. //The Data:
  2. class student {
  3.  
  4. char name[30];
  5. char gender[6];
  6. int age;
  7. etc....
  8. };
  9. class stud_Node
  10. {
  11.     student* s;                //points to a student object
  12.     stud_Node* next;      //points to next node
  13. etc...
  14. };
  15. class stud_list {
  16. int count;
  17. stud_Node *first, *last;
  18. };
  19.  
The reason for separating these is you may need to use the student in another program and the way things are set up, you will need to drag all the linked list code along with the student. A student is a student and should stand on its own.

Also, I assume this linked list is a class exercise otherwise you should be using the STL list<> template and not re-inventing the wheel.
Dec 11 '07 #2

P: 26
So you mean to say i must use struct before the class student?
Dec 12 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
So you mean to say i must use struct before the class student?
I don't know what you mean. In C++ a class and a struct are the same thing. The only difference is that unless you say otherwise, struct members are public by default whereas class members are private. Other than that, you can interchange the struct and class keywords.

May be you could expand a little on your question.
Dec 12 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.