472,780 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Student List Using insert_at_end method

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;

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

class stud_list {

int count;
student *first, *last;


stud_list() {
first=NULL; last=NULL;

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


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


void stud_list::insert_as_first(student *a) {


void student::display() {

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


int main()
student a;
return 0;
Dec 11 '07 #1
3 2631
9,208 Expert Mod 8TB
student() {
cout << "Name: "; cin >> name; cout << endl;
cout << "Gender: "; cin >> gender; cout << endl;
cout << "Age: "; cin >> age; cout << endl;
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. };
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

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 {
  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. };
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
So you mean to say i must use struct before the class student?
Dec 12 '07 #3
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

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...
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++...
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...
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...
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...
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 and it gives you...
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...
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...
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”. ...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: lllomh | last post by:
How does React native implement an English player?
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.