In: Computer Science
Below is my C++ program of a student record system. I have done the program through structures. Please do the same program using classes this time and dont use structures. Also, please make the menu function clean and beautiful if you can. So the output will look good at the end. Thanks.
#include<iostream>
#include<string>
using namespace std;
struct StudentRecord {
string name;
int roll_no;
string department;
StudentRecord *next;
};
StudentRecord *head = NULL;
StudentRecord *get_data()
{
//creating a temporary node in which we will store all
the student records and return the temporary node in the end of the
function
StudentRecord *rec = new StudentRecord;
cout << endl;
cout << "You chose option #1." <<
endl;
cout << "What is the student's name?: ";
cin >> rec->name;
cout << "What is the student's roll no?:
";
cin >> rec->roll_no;
cout << "What is the student's department?:
";
cin >> rec->department;
rec->next = head;
return rec;
}
void add_data(StudentRecord *current)
{
// We will store the address of the present head node
in the next field of the current node and later we will make the
current node as head node
current->next = head; // store the address of the
pointer head(second field)
head = current;
}
void display_data(double key)
{
cout << endl << endl << "Student
record: " << endl;
cout << "---------------------------" <<
endl;
// We will create a node named start and will iterate
it through the whole linked list and display the data
StudentRecord *start = head;
if (!start) {
cout << "No Data!" <<
endl;
return;
}
while (start != NULL)
{
if (start->roll_no == key)
{
cout <<
"Name : "<<start->name << endl;
cout <<
"Roll no : "<<start->roll_no << endl;
cout <<
"Department : "<<start->department << endl;
return;
}
start = start->next;
}
cout << "Record not found" << endl;
}
void search(double key)
{
// We will iterate the head through the linked list
until it finds the required variable or until the end of linked
list
while (head != NULL)
{
if (head->roll_no == key)
{
cout <<
"key found" << endl;
//
cout<<head->uin<<endl;
cout <<
"name = " << head->name << endl;
cout <<
"department = " << head->department << endl;
return;
}
head = head->next;
}
cout << "Key not found" << endl;
}
void UpdateData(double key)
{
StudentRecord *temp;
temp = head;
// We will iterate the head through the linked list
until it finds the required variable or until the end of linked
list
while (temp != NULL)
{
if (temp->roll_no == key)
{
cout <<
"What is the student's name?: ";
cin >>
temp->name;
cout <<
"What is the student's uin?: ";
cin >>
head->roll_no;
cout <<
"What is the student's date of birth?: ";
cin >>
temp->department;
cout <<
"Record updated..." << endl;
return;
}
temp = temp->next;
}
cout << "Key not found to update" <<
endl;
}
void DeleteRecord(double key)
{
StudentRecord *temp1, *temp2, *temp;
temp = head;
// We will iterate the head through the linked list
until it finds the required variable or until the end of linked
list
while (temp != NULL)
{
if (temp->roll_no == key)
{
head = temp->next;
cout << "Record Deleted successfully...." << endl;
return;
}
if (temp->next->roll_no ==
key)
{
temp1 =
temp->next;
temp2 =
temp1->next;
temp->next =
temp2;
cout <<
"Record Deleted Successfully..." << endl;
return;
}
temp = temp->next;
}
cout << "Key not found to Delete Record"
<< endl;
}
void processMenu()
{
// creating current node for StudentRecord
struct
StudentRecord *current = NULL;
int ser;
char choice = 0;
while (1) {
cout << endl << "What
would you like to do?" << endl;
cout <<
"==========================" << endl;
cout << "1. Enter a student
record. " << endl;
cout << "2. Display student
records. " << endl;
cout << "3. Search. "
<< endl;
cout << "4. Update. "
<< endl;
cout << "5. Delete. "
<< endl;
cout << "6. Exit. " <<
endl;
cin >> choice;
while (cin.get() != '\n');
if (choice == '1') {
system("cls");
current =
get_data();
add_data(current);
}
else if (choice == '2') {
cout <<
"Enter student uin to Display records" << endl;
cin >>
ser;
display_data(ser);
}
else if (choice == '3') {
system("cls");
cout <<
"Enter student uin to search for records" << endl;
cin >>
ser;
search(ser);
}
else if (choice == '4') {
system("cls");
cout <<
"Enter student uin to Update records" << endl;
cin >>
ser;
UpdateData(ser);
}
else if (choice == '5') {
system("cls");
cout <<
"Enter student uin to Delete records" << endl;
cin >>
ser;
DeleteRecord(ser);
}
else if (choice == '6') {
exit(1);
return;
}
else {
cout <<
"Allowed Selections are 1, 2, 3, 4, 5, and 6." << endl;
}
}
}
int main()
{
// Program starts execution from main block
cout << "Student Record Program." << endl
<< endl;
processMenu();
return 0;
}
Note: replaced struct with class and made all data members public
wherever I changed print statement are marked as //Changed here
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class StudentRecord { //replaced struct with class and made all data variables public
public:
string name;
int roll_no;
string department;
StudentRecord *next;
};
StudentRecord *head = NULL;
StudentRecord *get_data()
{
//creating a temporary node in which we will store all the student records and return the temporary node in the end of the function
StudentRecord *rec = new StudentRecord;
cout << endl;
//cout << "You chose option #1." << endl; Changed here
cout << "What is the student's name?: ";
cin >> rec->name;
cout << "What is the student's roll no?: ";
cin >> rec->roll_no;
cout << "What is the student's department?: ";
cin >> rec->department;
rec->next = head;
system("cls");//changed here
return rec;
}
void add_data(StudentRecord *current)
{
// We will store the address of the present head node in the next field of the current node and later we will make the current node as head node
current->next = head; // store the address of the pointer head(second field)
head = current;
}
void display_data(double key)
{
cout << endl << endl << "Student record: " << endl;
cout << "---------------------------" << endl;
// We will create a node named start and will iterate it through the whole linked list and display the data
StudentRecord *start = head;
if (!start) {
cout << "No Data!" << endl;
return;
}
while (start != NULL)
{
if (start->roll_no == key)
{
cout << "Name : "<<start->name << endl;
cout << "Roll no : "<<start->roll_no << endl;
cout << "Department : "<<start->department << endl;
cout << "---------------------------" << endl;
return;
}
start = start->next;
}
cout << "Record not found" << endl;
cout << "---------------------------" << endl;
}
void search(double key)
{
cout << "---------------------------" << endl;
// We will iterate the head through the linked list until it finds the required variable or until the end of linked list
while (head != NULL)
{
if (head->roll_no == key)
{
cout << "Record found" << endl;//changed here
// cout<<head->uin<<endl;
cout << "name = " << head->name << endl;
cout << "department = " << head->department << endl;
cout << "---------------------------" << endl;
return;
}
head = head->next;
}
cout << "Record not found" << endl;//changed here
cout << "---------------------------" << endl;
}
void UpdateData(double key)
{
StudentRecord *temp;
temp = head;
// We will iterate the head through the linked list until it finds the required variable or until the end of linked list
while (temp != NULL)
{
if (temp->roll_no == key)
{
cout << "What is the student's name?: ";
cin >> temp->name;
cout << "What is the student's roll number?: ";
cin >> head->roll_no;
cout << "What is the student's department?: ";//changed here
cin >> temp->department;
cout << "Record updated..." << endl;
return;
}
temp = temp->next;
}
cout << "Record not found to update" << endl;//changed here
}
void DeleteRecord(double key)
{
StudentRecord *temp1, *temp2, *temp;
temp = head;
// We will iterate the head through the linked list until it finds the required variable or until the end of linked list
while (temp != NULL)
{
if (temp->roll_no == key)
{
head = temp->next;
cout << "Record Deleted successfully...." << endl;
return;
}
if (temp->next->roll_no == key)
{
temp1 = temp->next;
temp2 = temp1->next;
temp->next = temp2;
cout << "Record Deleted Successfully..." << endl;
return;
}
temp = temp->next;
}
cout << "Record not found to Delete Record" << endl;//Changed here
}
void processMenu()
{
// creating current node for StudentRecord
StudentRecord *current = NULL;
int ser;
char choice = 0;
while (1) {
cout << "\n\t==========================" << endl;//added tab space
cout << "\t1. Enter a student record. " << endl;
cout << "\t2. Display student records. " << endl;
cout << "\t3. Search student record " << endl;
cout << "\t4. Update student record " << endl;
cout << "\t5. Delete student record " << endl;
cout << "\t6. Exit. " << endl;
cout << "\t==========================" << endl;
cout<<"\n\tPlease Enter Your Choice (1-6): ";
cin >> choice;
while (cin.get() != '\n');
if (choice == '1') {
system("cls");
current = get_data();
add_data(current);
}
else if (choice == '2') {
system("cls");//changed here
cout << "Enter student roll number to Display record" << endl;//changed here
cin >> ser;
display_data(ser);
}
else if (choice == '3') {
system("cls");
cout << "Enter student roll number to search for records" << endl;//changed here
cin >> ser;
search(ser);
}
else if (choice == '4') {
system("cls");
cout << "Enter student roll number to Update records" << endl;//changed here
cin >> ser;
UpdateData(ser);
}
else if (choice == '5') {
system("cls");
cout << "Enter student roll number to Delete records" << endl;//changed here
cin >> ser;
DeleteRecord(ser);
}
else if (choice == '6') {
exit(1);
return;
}
else {
cout << "Allowed Selections are 1, 2, 3, 4, 5, and 6." << endl;
}
}
}
int main()
{
// Program starts execution from main block
cout << "Student Record Program." << endl << endl;
processMenu();
return 0;
}
Output of main menu:
Student Record Program.
==========================
1. Enter a student record.
2. Display student records.
3. Search student record
4. Update student record
5. Delete student record
6. Exit.
==========================
Please Enter Your Choice (1-6):