Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write program in C++ Please include a screenshot of the following input: Here is

ID: 3572129 • Letter: W

Question

Write program in C++

Please include a screenshot of the following input:

Here is DB.txt :

1. Write a program to simulate a database. A database, represented by a class CDB, stores and retrieves information, organized into records. Records are retrieved using queries, high-level instructions, typically written in structured query language (SQL). The database stores records in a file "db.txt". Queries return sets of the names of the people in this file with ages between specified lower and upper bounds. At any one time, the results of at most 2 queries are kept in memory. These results are dynamically maintained using 2 pointers, pr and p.. When the results of a query are returned, pa is set to point to any previous results pointed to by p, and ph is set to point to new the results. If p already points to any results, then those results are deleted. The interface is made up of the functions described below. A user can construct a database. A user can query a database, receiving a set of results A user can print the results of a query pointed to by p A user can print the results of a query pointed to by p2

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<stdlib.h>
using namespace std;


class CDB{
string name;
int age;
vector<string> *p1, *p2;

public:
   CDB(){p1=NULL;p2=NULL;}

   CDB(string &n,int a){
       name.assign(n);
       age=a;
       p1=NULL;
       p2=NULL;
   }

   void construct(string &n,int a);
   void query(int lb,int ub);
   void printCurrentResult(void);
   void printPreviousResult(void);
  
};

void CDB::construct(string &n,int a){

ofstream ofile;

ofile.open("db.txt",std::ios_base::app);

if(!ofile.is_open())
{
   cout<<"Error in opening the database. Exiting..."<<endl;
   return;
}

ofile<<n<<","<<a<<endl;
ofile.close();

cout<<n<<","<<a<<endl;

}

void CDB::query(int lb,int ub)
{

ifstream ifile("db.txt");

if(!ifile.is_open()){
   cout<<"Error in querying the database. Exiting ..."<<endl;
   return;
}

string tmp;

if(p2!=NULL)
      delete p2;

p2=p1;

p1=new vector<string>();
string tt;

while(getline(ifile,tmp))
{
   int t=tmp.find(",");
   tt=tmp.substr(t+1,tmp.length());
   int num=atoi(tt.c_str());
   tmp=tmp.substr(0,t-1);
   if(num>=lb && num<ub)
       p1->push_back(tmp);
}
ifile.close();
}

void CDB::printCurrentResult(void)
{
if(p1==NULL) return;
for(vector<string>::iterator iter=p1->begin();iter!=p1->end();iter++)
   cout<<*iter<<endl;
}

void CDB::printPreviousResult(void)
{
if(p2==NULL) return;
for(vector<string>::iterator iter=p2->begin();iter!=p2->end();iter++)
   cout<<*iter<<endl;
}

int main(void){

CDB db;
int n;
string name;
int age;

while(1){
   cout<<"1.Construct DB"<<endl;
   cout<<"2.Query DB"<<endl;
   cout<<"3.Exit"<<endl;
   cin>>n;

    switch(n){
   case 1:
       cout<<"Enter name and age:"<<endl;
       cin>>name>>age;
       db.construct(name,age);
       break;
   case 2:
       cout<<"Enter lowerbound and upper bound:"<<endl;
       cin>>age>>n;
       db.query(age,n);
       cout<<"Current Result"<<endl;
       db.printCurrentResult();
       cout<<"Previous Result"<<endl;
       db.printPreviousResult();
       break;
   default:
       return 0;
   }
}
  

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote