Below is my C++ program. There is a function to add data, display it and search the data. Just add two more functions to this porgram. One function to update the student data and second function to delete the data. Also, change the display function little bit. Right now, it shows all data entered. Make it so you have to put the uin number and it only displays the data of that specific student.
#include <iostream>
#include <string>
using namespace std;
struct StudentRecord {
string firstName;
string lastName;
int oduUin;
string dateOfBirth;
double gpa;
StudentRecord *next;
};
StudentRecord *head = NULL;
void display_data()
{
cout << endl << endl << "Listing all
student records: " << 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) {
cout << start->firstName
<< endl;
cout << start->lastName
<< endl;
cout << start->oduUin
<< endl;
cout << start->dateOfBirth
<< endl;
cout << start->gpa
<< endl << endl;
start = start->next;
}
}
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 first name?:
";
cin >> rec->firstName;
cout << "What is the student's last name?:
";
cin >> rec->lastName;
cout << "What is the student's uin?: ";
cin >> rec->oduUin;
cout << "What is the student's date of birth?:
";
cin >> rec->dateOfBirth;
cout << "What is the student's GPA?: ";
cin >> rec->gpa;
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 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->oduUin == key)
{
cout <<
"key found" << endl;
//
cout<<head->uin<<endl;
cout <<
"first name = " << head->firstName << endl;
cout <<
"last name = " << head->lastName << endl;
cout <<
"date of birth = " << head->dateOfBirth <<
endl;
cout <<
"gpa = " << head->gpa << endl;
return;
}
head = head->next;
}
cout << "Key not found" << endl;
}
void processMenu()
{
// creating current node for StudentRecord
struct
StudentRecord *current = NULL;
int ser;
char choice = 0;
while (true) {
cout << endl << "What
would you like to do?" << endl;
cout <<
"==========================" << endl;
cout << "1. Enter a student
record. " << endl;
cout << "2. List all student
records. " << endl;
cout << "3. Exit program. "
<< endl;
cout << "4. Search. "
<< endl;
cin >> choice;
while (cin.get() != '\n');
if (choice == '1') {
current =
get_data();
add_data(current);
}
else if (choice == '2') {
display_data();
}
else if (choice == '3') {
exit(1);
return;
}
else if (choice == '4') {
cout <<
"Enter student uin to search for records" << endl;
cin >>
ser;
search(ser);
}
else {
cout <<
"Allowed Selections are 1, 2, and 3!" << endl;
}
}
}
int main()
{
// Program starts execution from main block
cout << "Student Record Program." << endl
<< endl;
processMenu();
// calling process function which inturn calls create
and display functions
system("pause");
return 0;
}
#include <iostream.h> // remove .h
#include <string.h> // remove .h
#include<stdlib.h> //remove .h
//using namespace std;
struct StudentRecord {
char firstName[50]; // for linux use string firstName
char lastName[50]; // for linux use string lastName
int oduUin;
char dateOfBirth[50]; //for linux use string dateOfBirth
double gpa;
StudentRecord *next;
};
StudentRecord *head = NULL;
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->oduUin == key)
{
cout << "What is the student's first name?:
";
cin >> temp->firstName;
cout << "What is the student's last name?:
";
cin >> temp->lastName;
// cout << "What is the student's uin?: "; // in
case you want to update uin
// cin >> head->oduUin;
cout << "What is the student's date of birth?:
";
cin >> temp->dateOfBirth;
cout << "What is the student's GPA?: ";
cin >> temp->gpa;
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->oduUin == key)
{
head = temp->next;
cout<<"Record Deleted successfully...."<<endl;
return;
}
if (temp->next->oduUin == 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 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->oduUin == key)
{
cout << start->firstName << endl;
cout << start->lastName << endl;
cout << start->oduUin << endl;
cout << start->dateOfBirth << endl;
cout << start->gpa << endl << endl;
return;
}
start = start->next;
}
cout << "Record not found" << endl;
}
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 first name?: ";
cin >> rec->firstName;
cout << "What is the student's last name?: ";
cin >> rec->lastName;
cout << "What is the student's uin?: ";
cin >> rec->oduUin;
cout << "What is the student's date of birth?: ";
cin >> rec->dateOfBirth;
cout << "What is the student's GPA?: ";
cin >> rec->gpa;
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 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->oduUin == key)
{
cout << "key found" << endl;
// cout<<head->uin<<endl;
cout << "first name = " <<
head->firstName << endl;
cout << "last name = " <<
head->lastName << endl;
cout << "date of birth = " <<
head->dateOfBirth << endl;
cout << "gpa = " << head->gpa <<
endl;
return;
}
head = head->next;
}
cout << "Key not found" << 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. Exit program. " << endl;
cout << "4. Search. " << endl;
cout << "5. Update. " << endl;
cout << "6. Delete Record. " << endl;
cin >> choice;
while (cin.get() != '\n');
if (choice == '1') {
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') {
exit(0); // in linux exit(1)
return;
}
else if (choice == '4') {
cout << "Enter student uin to search for
records" << endl;
cin >> ser;
search(ser);
}
else if (choice == '5') {
cout << "Enter student uin to Update records"
<< endl;
cin >> ser;
UpdateData(ser);
}
else if (choice == '6') {
cout << "Enter student uin to Delete records"
<< endl;
cin >> ser;
DeleteRecord(ser);
}
else {
cout << "Allowed Selections are 1, 2, and 3!"
<< endl;
}
}
}
int main()
{
// Program starts execution from main block
cout << "Student Record Program." << endl <<
endl;
processMenu();
// calling process function which inturn calls create and display
functions
// system("pause"); //remove comment in your code
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.