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
o 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:
o 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
o Display the details of a single student. The student is retrieved by their name
o Display the entire database, creating a numbered list of all students
What I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#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;
}
|