Hey all!

I'm trying to write a program that simulates a student database. From the user, it takes the number of students there are, their names, student ID's, and majors. I'm having a runtime problem with the code I have and was curious to know if anyone can help. It compiles fine but it crashes when it displays the entire database and does not loop again. Any help is appreciated!

Le prompt:

Define a class Student that has as its members the s_name, s_id, and major all of type string. The class also has:

-A default constructor
-A constructor that takes as input two strings, one for initializing each member variable
-Appropriate accessors and mutators
-A function or overloaded operator for printing the details of the student on the screen

example:
-Name:(displays name here)
-Student ID: (displays ID # here)
-Major: (displays major here)

The main program should:
-Ask the user to input (from the keyboard) the number of students in the database
-Create a dynamic array of type Student and input the information for each student, as given by the user. You need to use appropriate member functions to do that
-Once you populate the database (i.e. stored all information for all Students), the program should allow you to:

-Update the database by changing the major of any student

Simple option: The user gives the number of the students (i.e. position in the database) and the new major

-Display the details of a single student. The student is retrieved by their name
-Display the entire database, creating a numbered list of all students

What I have:

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

class student
{
    public:
        student()
        {
        name = '0';
        s_id = '0';
        major = '0';
        }
        student(string n, string m, string j);

        friend ostream & operator <<(ostream &os, const student& stu)
        {
            os << "Name: " << stu.name << "\n" << "Student ID: " << stu.s_id;
            return os;
        }

        string name;
        string s_id;
        string major;
};

student::student(string n, string m, string j)
{
    name = n;
    s_id = m;
    major = j;
}


int main(void)
{   

    cout << "Please enter the number of students in the database: ";
    int size;
    cin >> size;
    size = size+1;
    student *a;
    a = new student[size];
    int i = 1;
    for(i=1;i<size;i++)
    {
        cout << "Enter name, student ID, and major... \n(follow each input with the 'enter' key until new prompt is seen):" << endl;
        string sname;
        string snumber;
        string smajor;
        cin >> sname >> snumber >> smajor;
        student test(sname, snumber, smajor);
        a[i] = test;
    }
    cout << "Would you like to edit any data? (yes or no): ";
    string yes = "yes";
    string te;
    cin >> te;
    string no = "no";
    if(te == yes)
    {
        cout << endl << "Would you like to search by the student's name instead of the ID? (yes or no): ";
        string fat;
        cin >> fat;
        if(fat == yes)
        {
            string ttname;
            cout << endl << "What is the student's name?: " << endl;
            cin >> ttname;
            int j = 1;
            for(j=1;j<size;j++)
            {
                student temp2;
                temp2 = a[j];
                if(temp2.name == ttname)
                {
                    cout << "You want to edit?: " << endl << temp2 << endl;
                    cout << "What is the new major?: ";
                        string neww;
                        cin >> neww;
                        temp2.major = neww;
                        a[j] = temp2;
                        cout << "The new file looks like..." << endl;
                        cout << temp2;
                }
            }
    }
    if(fat == no)
    {
        cout << "What is the student's ID that you would like to edit?: ";
        int tem;
        cin >> tem;
        cout << "You would like to edit: " << endl << a[tem];
        cout << endl;
        cout << "What is the new major?: ";
        string newm;
        cin >> newm;
        student tempp;
        tempp = a[tem];
        tempp.major = newm;
        a[tem] = tempp;
        cout << "The new file looks like..." << endl;
        cout << tempp;
    }
}

cout << endl << "Would you like to show the details of a student? (yes or no): ";
    string response;
    cin >> response;
    if(response == yes)
    {
        string tempname;
        cout << endl << "What is their name: " << endl;
        cin >> tempname;
        int j = 1;
        for(j = 1;j<size;j++)
        {
            student temp1;
            temp1 = a[j];
            if(temp1.name == tempname)
            {
                cout<<temp1<<endl;
            }
        }
    }
    cout << endl << "Would you like to display the entire database (yes or no): ";
    string testing;
    cin >> testing;
    if(testing == yes)
    {
        int k = 0;
        for(k = 1;k<size;k++)
        {
            cout << k << ".)" << endl << a[k] << endl;
        }
    }
delete a;
return 0;
}

Anyways, once again, thanks for any help!

Seems fine: this is my output.

Please enter the number of students in the database: 2
Enter name, student ID, and major... 
(follow each input with the 'enter' key until new prompt is seen):
1
2
3
Enter name, student ID, and major... 
(follow each input with the 'enter' key until new prompt is seen):
1
2
3
Would you like to edit any data? (yes or no): no

Would you like to show the details of a student? (yes or no): no

Would you like to display the entire database (yes or no): yes
1.)
Name: 1
Student ID: 2
2.)
Name: 1
Student ID: 2

Perhaps this application is done in a hurry, but I would suggest some modifications to the current class. You have the base class of the program, which is the Student class. I would suggest creating another class that manages more than one student, using maybe some stl conainers. You could create anther class for validating the client, and another one to get the user input and send informations to the repository adding students to the container.

So my suggested architecture would look like this:

                        Student Class
                            ^
                            |
                Student Repository(manages students,
                                   adds them to the container,
                                   sorts them,
                                   find a student by a given identifier,
                                   update a student,
                                   removes a student etc.)
                                 ^  
                                 |  
                    Student controller(manages the user's input,
                           sends information to Repository)
                                 ^
                                 |
                    Student validator(validates the input from the user)
                                 ^
                                 |
                    Student ui(gets the input from the user)

This would be a classic aproach to your problem, there are plenty more out there.

That's really weird. I was getting a forced shut down of the program...

edit: Sorry, I didn't even notice the rest of your post! I really appreciate the response. Very informative, thanks so much. I will try to do what you suggested and get it to function a little more efficiently.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.