Click here to Skip to main content
15,904,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to read student and teacher details from different files and using two different classes from both student and teacher.When i compile my program it doesn't show any error but i put break in main and then run it. while reading file in first while condition break point stops at
S.create_node(name, course_code, marks, cgpa);

any idea how can i fix this problem since i am very new to this programming concept.

What I have tried:

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>

using namespace std;


struct Node
{
    string s_name;
    string s_course_code;
    int s_marks;
    float s_cgpa;
    string t_name;
    int t_age;
    int t_Class;
    string t_subject;
    Node *next;
};

class student
{
private:
    Node *head;
public:
    student();
    void create_node(string name, string course_code, int marks, float cgpa);
    void delete_node(int node_no);
    void display();
    void search_name(string name);
};

student::student()
{
    head = NULL;
}

void student::create_node(string name, string course_code, int marks, float cgpa)
{
    int size = 0;
    Node *temp = new Node;
    temp->s_name = name;
    temp->s_course_code = course_code;
    temp->s_marks = marks;
    temp->s_cgpa = cgpa;
    temp->next = NULL;
    if (!head)
    {
        head = temp;
    }
    else
    {
        Node *t = head;
        while (t->next != NULL)
        {
            t = t->next;
        }
        temp = t;
        t->next = temp;
        t = t->next;
    }
    size++;
}

void student::delete_node(int node_no)
{
    int counter = 0;
                                            // Check if node is exist
    if (node_no > counter)
    {
        cout << "No such node is exist";
    }

    else
    {

        Node *temp1;                            // create a temporary node
        temp1 = (Node*)malloc(sizeof(Node));    // allocate space for node
        temp1 = head;                       // transfer the address of 'head' to 'temp1'

        Node *old_temp;                         // create a temporary node
        old_temp = (Node*)malloc(sizeof(Node)); // allocate space for node
        old_temp = temp1;                       // transfer the address of 'temp1' to 'old_temp'

                                                // Check node number is 1
        if (node_no == 1)
        {
            head = temp1->next;                  // transfer the address of 'temp1->next' to 'head'
            free(temp1);
            counter--;
            cout << node_no << " node of the Linked List is deleted" << endl;
        }
        else
        {


            // Go to the node number of the node
            for (int i = 1; i < node_no; i++)
            {

                old_temp = temp1;               // store previous node
                temp1 = temp1->next;             // store current node

            }

            old_temp->next = temp1->next;     // transfer the address of 'temp1->next' to 'old_temp->next'
            free(temp1);

            counter--;
            cout << node_no << " node of the Linked List is deleted" << endl;
        }
    }

}

void student::display()
{
    if (!head)
    {
        cout << "File is Empty.........................." << endl;
    }
    else
    {
        Node *t = head;
        //while(t->next!=NULL)
        //t=t->next;
        int count = 1;
        cout << endl << endl;
        while (t != NULL)
        {
            cout << "[" << count << "]=>" << endl;
            cout << "\t\tName:" << t->s_name << endl;
            cout << "\t\tCourse Code:" << t->s_course_code << endl;
            cout << "\t\tMarks:" << t->s_marks << endl;
            cout << "\t\tCGPA:" << t->s_cgpa << endl;
            count++;
            t = t->next;
        }

    }
}


void student::search_name(string name)
{
    if (!head)
    {
        cout << "File is Empty.........................." << endl;
    }
    else
    {
        Node *t = head;
        //while(t->next!=NULL)
        //t=t->next;
        int count = 1;
        cout << endl << endl;
        while (t != NULL)
        {
            if (t->s_name == name)
            {
                cout << "[" << count << "]=>" << endl;
                cout << "\t\tName:" << t->s_name << endl;
                cout << "\t\tCourse Code:" << t->s_course_code << endl;
                cout << "\t\tMarks:" << t->s_marks << endl;
                cout << "\t\tCGPA:" << t->s_cgpa << endl;

            }

            count++;
            t = t->next;
        }

    }
}


class teacher
{

private:

    Node *head;

public:

    teacher();
    void create_node(string name, string subject, int Class, int age);
    void delete_node(int node_no);
    void display();
    void search_name(string name);

};

teacher::teacher()
{
    head = NULL;
}

void teacher::create_node(string name, string subject, int Class, int age)
{
    int size = 0;
    Node *temp = new Node;
    temp->t_name = name;
    temp->t_subject = subject;
    temp->t_Class = Class;
    temp->t_age = age;
    temp->next = NULL;

    if (!head)
    {
        head = temp;
    }
    else
    {
        Node *t = head;
        while (t->next != NULL)
        {
            t = t->next;
        }
        temp = t;
        t->next = temp;
        t = t->next;
    }
    size++;
}

void teacher::delete_node(int node_no)
{
    int counter = 0;
    // Check if node is exist
    if (node_no > counter)
    {
        cout << "No such node is exist";
    }

    else
    {

        Node *temp1;                            // create a temporary node
        temp1 = (Node*)malloc(sizeof(Node));    // allocate space for node
        temp1 = head;                       // transfer the address of 'head' to 'temp1'

        Node *old_temp;                         // create a temporary node
        old_temp = (Node*)malloc(sizeof(Node)); // allocate space for node
        old_temp = temp1;                       // transfer the address of 'temp1' to 'old_temp'

                                                // Check node number is 1
        if (node_no == 1)
        {
            head = temp1->next;                  // transfer the address of 'temp1->next' to 'head'
            free(temp1);
            counter--;
            cout << node_no << " node of the Linked List is deleted" << endl;
        }
        else
        {


            // Go to the node number of the node
            for (int i = 1; i < node_no; i++)
            {

                old_temp = temp1;               // store previous node
                temp1 = temp1->next;             // store current node

            }

            old_temp->next = temp1->next;     // transfer the address of 'temp1->next' to 'old_temp->next'
            free(temp1);

            counter--;
            cout << node_no << " node of the Linked List is deleted" << endl;
        }
    }
}

void teacher::display()
{
    if (!head)
    {
        cout << "File is Empty.........................." << endl;
    }
    else
    {
        Node *t = head;
        //while(t->next!=NULL)
        //t=t->next;
        int count = 1;
        cout << endl << endl;
        while (t != NULL)
        {
            cout << "[" << count << "]=>" << endl;
            cout << "\t\tName:" << t->t_name << endl;
            cout << "\t\tSubject" << t->t_subject << endl;
            cout << "\t\tClass" << t->t_Class << endl;
            cout << "\t\tAge:" << t->t_age << endl;
            count++;
            t = t->next;
        }

    }
}

void teacher::search_name(string name)
{
    if (!head)
    {
        cout << "File is Empty.........................." << endl;
    }
    else
    {
        Node *t = head;
        //while(t->next!=NULL)
        //t=t->next;
        int count = 1;
        cout << endl << endl;
        while (t != NULL)
        {
            if (t->t_name == name)

            {
                cout << "[" << count << "]=>" << endl;
                cout << "\t\tName:" << t->t_name << endl;
                cout << "\t\tSubject:" << t->t_subject << endl;
                cout << "\t\tClass" << t->t_Class << endl;
                cout << "\t\tAge:" << t->t_age << endl;
            }

            count++;
            t = t->next;
        }

    }
}

int main()
{
    student S;
    teacher T;
    string T_name;
    string subject;
    int age;
    int Class_teacher;
    string course_code;
    int marks;
    float cgpa;
    string name;
    int ch;

    ifstream fin,fin1,fin2,fin3,fin4, fin5, fin6, fin7;

    fin.open("s_name.txt");
    fin1.open("s_course.txt");
    fin2.open("s_marks.txt");
    fin3.open("s_cgpa.txt");
    fin4.open("t_name.txt");
    fin5.open("t_subject.txt");
    fin6.open("t_class.txt");
    fin7.open("t_age.txt");


    while (!fin.eof() && !fin1.eof() && !fin2.eof() && !fin3.eof() && !fin4.eof() && !fin5.eof() && !fin6.eof() && !fin7.eof())
    {
        fin >> name;
        fin1 >> course_code;
        fin2 >> marks;
        fin3 >> cgpa;
        fin4 >> T_name;
        fin5 >> subject;
        fin6 >> Class_teacher;
        fin7 >> age;
        S.create_node(name, course_code, marks, cgpa);
        T.create_node(T_name, subject, Class_teacher, age);
    }

    while (1)

    {
        cout << "-------------------STUDENT MANAGEMENT FUNCTION----------------------------" << endl;
        cout << "Enter 1 to Create_Student_Record:" << endl;
        cout << "Enter 2 to Display the Student_Record:" << endl;
        cout << "Enter 3 to Find the Student with Name:" << endl;
        cout << "Enter 4 to Delete the Record of the Student:" << endl;
        cout << "-------------------TEACHER MANAGEMENT FUNCTION----------------------------" << endl;
        cout << "Enter 5 to Create_Teacher_Record:" << endl;
        cout << "Enter 6 to Display the Teacher_Record:" << endl;
        cout << "Enter 7 to Find the Teacher with Name:" << endl;
        cout << "Enter 8 to Delete the Record of the Teacher:" << endl;
        cout << "Enter 9 for Exit:" << endl;

        cin >> ch;

        if (ch == 1)

        {
            cout << "Enter the Name:" << endl;
            cin >> name;
            cout << "Enter the Course Code:" << endl;
            cin >> course_code;
            cout << "Enter the marks:" << endl;
            cin >> marks;
            cout << "Enter the CGPA:" << endl;
            cin >> cgpa;
            S.create_node(name, course_code, marks, cgpa);

        }

        else if (ch == 2)
        {
            S.display();
        }


        else if (ch == 3)
        {

            string x;
            cout << "Enter the name:" << endl;
            cin >> x;
            S.search_name(x);

        }

        else if (ch == 4)
        {
            int x;
            cout << "Enter the Student Location-Number:" << endl;
            cin >> x;
            S.delete_node(x);
            cout << "Student at Location " << x << " is deleted" << endl;

        }

        else if (ch == 5)
        {

            cout << "Enter the Name:" << endl;
            cin >> T_name;
            cout << "Enter the subject:" << endl;
            cin >> subject;
            cout << "Enter the class:" << endl;
            cin >> Class_teacher;
            cout << "Enter the age" << endl;
            cin >> age;
            T.create_node(T_name, subject, Class_teacher, age);

        }

        else if (ch == 6)
        {

            T.display();

        }

        else if (ch == 7)
        {

            string x;
            cout << "Enter the name:" << endl;
            cin >> x;
            T.search_name(x);

        }

        else if (ch == 8)
        {

            int x;
            cout << "Enter the Teacher Location-Number:" << endl;
            cin >> x;
            T.delete_node(x);
            cout << "Teacher at Location " << x << " is deleted" << endl;

        }


        else if (ch == 9)
        {
            exit(1);
        }

        else
        {
            cout << "Enter the valid Input......" << endl;
        }
        system("pause");
        system("cls");
    }

    return 0;
}
Posted
Updated 21-Jun-18 3:28am

That's what a break point is there for - to stop your program executing so you can look at what is going on, and step though your code to find problems.

How you use the debugger depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 13784265 21-Jun-18 9:20am    
thanks sir.
i am using visual studio and i am having this program on this compiler. but when i run this same program it run properly on Cfree.why is that so ?
The problem is in this code part:
C++
temp = t;
t->next = temp;
t = t->next;
When execution reaches that code, t is the last node. By assigning temp to t, it is effectively the same as
C++
t->next = t;
which results in an endloss loop when searching for the last node the next time. The solution is to remove the first and last (useless) statement from the above block.

But there are more problems in your code (I did not checked all the code):
  • You are mixing C++ memory allocation (new) with C allocations (malloc). Never do such.
  • You have count[er] and size local variables in your functions. If you intend to use those globally you have to make them static or (better) being class members.
  • The local (see above) counter variable in your delete_node() function is initialised with zero so that the functions always returns when passing a positive node_no.

Finally note that a compilation without errors does not mean that the program is working as intended or did not even crash. In fact, most errors are not catched by the compiler.

A tip: Use a high warning level (/W4 with VS or -Wall with GCC). That will find at least some possible problems.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900