d file handling in search and delete record to this C++ Program. #include #include #include #include #include using namespace std; bool check = true; struct no

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Add file handling in search and delete record to this C++ Program. 

#include<iostream>
#include<fstream>
#include<cctype>
#include<sstream>
#include<string>
using namespace std;
bool check = true;
struct node    //structure of node //
{
 int rollNo;
 char name[20];
 char address[40];
 char gender[20];
 char department[20];
 int year;
 char dob[20];
 node *next;
} *head, *lastptr;

void add()    //Adds record of student//
{
    //initialization was not proper
    //changed it to the correct one
 node *p = (struct node*) malloc(sizeof(struct node));
 cout << "Enter ID number of student:" << endl;
 cin >> p->rollNo;
 fflush(stdin);
 cout << "Enter name of student:" << endl;
 gets(p->name);
 fflush(stdin);
 cout << "Enter address of student:" << endl;
 gets(p->address);
 fflush(stdin);
 cout << "Enter birthday of student:" << endl;
 gets(p->dob);
 fflush(stdin);
 cout << "Enter gender of student:" << endl;
 gets(p->gender);
 fflush(stdin);
 cout << "Enter department of student:" << endl;
 gets(p->department);
 fflush(stdin);
 cout << "Enter Training year of student:" << endl;
 cin >> p->year;
 fflush(stdin);
 p->next = NULL;

 if (check) {
  head = p;
  lastptr = p;
  check = false;
 } else {
  lastptr->next = p;
  lastptr = p;
 }

//  Writing data to file -------------------------------------------------------
// Remove the '------' lines if not required
 ofstream file("student_data.txt");
 file << "------------------------------------------------------" << endl;
 file << "Student Id: " << lastptr->rollNo << endl;
 file << "Student Name: " << lastptr->name << endl;
 file << "Student Address: " << lastptr->address << endl;
 file << "Student DOB: " << lastptr->dob << endl;
 file << "Student Gender: " << lastptr->gender << endl;
 file << "Student Department: " << lastptr->department << endl;
 file << "Student Training Year: " << lastptr->year << endl;
 file << "------------------------------------------------------" << endl
   << endl << endl;
 // ------------------------------

 cout << endl << "Recored Entered" << endl;
}
void modify()   //modifies record of student//
{
 node *ptr = (struct node*) malloc(sizeof(struct node));
 node *prev = (struct node*) malloc(sizeof(struct node));
 node *current = (struct node*) malloc(sizeof(struct node));
 int roll_no;
 cout << "Enter ID number to search:" << endl;
 cin >> roll_no;
 prev = head;
 current = head;
 while (current->rollNo != roll_no) {
  prev = current;
  current = current->next;
 }
 ptr = new node;
 fflush(stdin);

 cout << "Enter ID number of student:" << endl;
 cin >> ptr->rollNo;
 fflush(stdin);
 cout << "Enter name of student:" << endl;
 gets(ptr->name);
 fflush(stdin);
 cout << "Enter address of student:" << endl;
 gets(ptr->address);
 fflush(stdin);
 cout << "Enter birthday of student:" << endl;
 gets(ptr->dob);
 fflush(stdin);
 cout << "Enter gender of student:" << endl;
 gets(ptr->gender);
 fflush(stdin);
 cout << "Enter department of student:" << endl;
 gets(ptr->department);
 fflush(stdin);
 cout << "Enter Training year of student:" << endl;
 cin >> ptr->year;
 fflush(stdin);
 prev->next = ptr;
 ptr->next = current->next;
 current->next = NULL;
 delete current;
 cout << endl << "Recored Modified";
}
void search()   //searches record of student//
{
 node *prev = (struct node*) malloc(sizeof(struct node));
 node *current = (struct node*) malloc(sizeof(struct node));
 int roll_no;
 cout << "Enter ID Number to search:" << endl;
 cin >> roll_no;
 prev = head;
 current = head;
 while (current->rollNo != roll_no) {
  prev = current;
  current = current->next;
 }

 cout << "\nID No:";
 cout << current->rollNo;
 cout << "\nname: ";
 puts(current->name);
 cout << "\nAddress: ";
 puts(current->address);
 cout << "\nGender: ";
 puts(current->gender);
 cout << "\ndepartment: ";
 puts(current->department);
 cout << "\nYear: ";
 cout << current->year;
 cout << "\nDate of birth: ";
 puts(current->dob);
}
void del() {
 node *ptr = (struct node*) malloc(sizeof(struct node));
 node *prev = (struct node*) malloc(sizeof(struct node));
 node *current = (struct node*) malloc(sizeof(struct node));
 int roll_no;
 cout << "Enter ID Number to Delete:" << endl;
 cin >> roll_no;
 prev = head;
 current = head;
 while (current->rollNo != roll_no) {
  prev = current;
  current = current->next;
 }
 prev->next = current->next;
 current->next = NULL;
 //there was error in the line
 //added square brackets
 delete[] current;
 cout << endl << "Recored Deleted";
}

int main() {
 int x;

 do {
  cout << "1--->Press '1' to add New record:" << endl;
  cout << "2--->Press '2' to search a record:" << endl;
  cout << "3--->Press '3' to modify a record:" << endl;
  cout << "4--->Press '4' to delete a record:" << endl;
  cout << "5--->Press '5' to exit:" << endl;
  cin >> x;
  switch (x) {
  case 1:
   system("cls");
   add();
   break;
  case 2:
   system("cls");
   search();
   break;
  case 3:
   system("cls");
   modify();
   break;
  case 4:
   system("cls");
   del();
   break;
  case 5:
   exit(0);
   break;
  default:
   cout << "Enter a valid option. \n";
   break;
  }
 } while (1);
}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education