473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Student List Using insert_at_end method

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
3 2666
weaknessforcats
9,208 Expert Mod 8TB
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
Synapse
26
So you mean to say i must use struct before the class student?
Dec 12 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

17
by: Sue | last post by:
<html> Is there someone here that can help me validate the period as the fourth from the last character in an email address. There is other information and validation on the form I have to do but...
4
by: mchoya | last post by:
I'm so frustrated. I'm beginning school next week and I have been working on a simple program for several days now without being able to complete it. (Though I have brushed up on a lot of C++...
6
by: laj | last post by:
HI, Excuse this new question, I am trying to impress my wife by putting a db together for her dance studio. I put a table with all students new and old with fields including name and address and...
1
by: Pieter Linden | last post by:
Hi, I think the subject line pretty much says it all... Say I have a students-classes database and I add a twist. I want to filter what courses a student can take by comparing the courses he...
1
by: Louis | last post by:
Hi, I am a first time poster here. I have been given an assignment to do from college and it is ot create a student gradebook. I have planned all the tables etc. to what I believe to be correct. I...
1
by: Student | last post by:
Hey I need to write an application that will scan my network, explicitly my network group. So it needs to do something like a "nbtscan" (you give it a parameter like 192.168.0.0/24 and it gives you...
2
by: sallyk07 | last post by:
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the...
4
by: Dave White | last post by:
Hello Everyone, I have created two tables to track my students' lessons. Each student is responsible for most, but not all. of the lessons. I've tried a junction table but I can't figure out...
11
by: xxbabysue123xx | last post by:
Heres the problem: Create a class Student with instance data name, studentNumber, class (where class is a String containing one of the following: “Freshman”, “Sophomore”, “Junior”, “Senior”. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.