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

I need help coding in C++ on the Source.cpp for Add an item. Type A Update an it

ID: 3763839 • Letter: I

Question

I need help coding in C++ on the Source.cpp for

Add an item.                                      Type A

Update an item.                               Type U

Delete an item.                Type D

********************************************************************************

Source.cpp below

********************************************************************************

#include "Header.h"
#include <iostream>
#include <fstream>

int main()

{
   database myDatabase;
   record myRecord;
   bool done = false;
   char answer;
   fstream myFile;


   do
   {
       cout << endl << "Chose from the following menu: " << endl << endl;
       cout << "Read from a file. Type R" << endl;
       cout << "Print entire database. Type P" << endl;
       cout << "Add an item. Type A" << endl;
       cout << "Update an item. Type U" << endl;
       cout << "Delete an item. Type D" << endl;
       cout << "Quit. Type Q" << endl << endl;
       cout << "Please tyep your selection: ";
       cin >> answer;

       switch (answer)
       {
       case 'r':
       case 'R':
           myFile.open("StudentData.txt", ios::in);

           if (myFile.is_open())
           {
               while (!myFile.eof())
               {
                   myFile >> myRecord.idField;
                   myFile >> myRecord.LName;
                   myFile >> myRecord.FName;
                   myFile >> myRecord.satScore;
                   myFile >> myRecord.gender;
                   myFile >> myRecord.GPA;

                   if (!myDatabase.addRecord(myRecord))
                   {
                       cout << myRecord.idField << " " << myDatabase.getmsg() << endl;

                   }
               }

               myFile.close();
               cout << "Read from file was successful." << endl<<endl<<endl;
           }
           else
           {
               cout << "Could not open file." << endl;

           }
          
           break;

       case 'p':
       case 'P':
       if (myDatabase.resetIndex())
       {
           do
           {
               myRecord = myDatabase.getRecord();
               cout << myRecord.idField<<" " ;
               cout << myRecord.LName <<" ";
               cout << myRecord.FName << " ";
               cout << myRecord.satScore << " " ;
               cout << myRecord.gender << " " ;
               cout << myRecord.GPA << " " << endl;

           }

           while (myDatabase.incrIndex());
           cout << "Print complete." << endl << endl;
       }
       else
       {
           cout << myDatabase.getmsg() << endl<< endl;

       }

           break;

       case 'a':
       case 'A':
           bool database::addRecord(record myRecord);
           break;
       case 'd':
       case 'D':

           break;
       case 'u':
       case 'U':

           break;
       case 'q':
       case 'Q':

           done = true;
           break;

       default:
           cout << "Incorrect response.";
           break;
       }
      
   }
      
       while (!done);


}

********************************************************************************

Headder.h below

********************************************************************************

#pragma once
//Database to contain 5 fields (ID Field, LName, FName, SAT Score, GPA)
//everything in a struct is public, everything in a class is private, by default

#include <string>
#include <vector>

using namespace std;

class record
{
public:

   unsigned int idField; //range check all applicable fields
   string LName;
   string FName;
   unsigned int satScore;
   float GPA;
   char gender;

};

class database
{

private:

   string msg;
   //record storage[100]; storage array for 'record' (old way)
   vector <record> storage; //new way
   int index;

public:

   database(); //this is our constructor
   ~database(); //this is our destructor, wipes out storage once it's not needed any more
   bool addRecord (record myRecord);

   bool updateRecord (record myRecord);

   bool deleteRecord(record myRecord);

   //viewRecord print out for main function
   bool resetIndex();

   bool incrIndex();

   bool decrIndex();

   record getRecord();
//if index is at the end, how can a record be retrieved...find solution

   bool findRecord(record myRecord);

   string getmsg();

};

********************************************************************************

Headder.cpp below

********************************************************************************


#include "Header.h"


database::database() //this is our constructor
{
   msg = "Database is empty";
   index = -1;

}
database::~database() //this is our destructor, wipes out storage once it's not needed any more
{


}
bool database::addRecord(record myRecord)
{
   bool status;
   bool done = false;

   if (storage.empty())
   {
       index = 0;
       //storage[index] = myRecord <---input if working with array

       storage.push_back(myRecord); //first record = 0 location

       status = true;
       msg = "Record successfully added!";
       done = true;
   }
     
   //checking to see if id field has duplicate
   if (!done)
   {
       unsigned int i;
       for (i = 0; (i < (storage.size())) && (myRecord.idField > storage[i].idField); ++i);
       if (i < (storage.size()) && myRecord.idField == storage[i].idField)
       {
           done = true;
           status = false;
           msg = "Duplicate record. Not added to database.";
       }
       else
       {
           done = false;
           //status = true; not true till element/record is inserted
       }
   }
  
   if(!done)
   {
       storage.push_back(myRecord); //adds record to end of vector

       for (unsigned int i = storage.size() - 1;
       i > 0 && (storage[i].idField < storage[i - 1].idField)
           ; --i) //for loop for sorting records numerically
       {
           record tempRecord = storage[i];
           storage[i] = storage[i - 1];
           storage[i - 1] = tempRecord;

       }
       index = 0;
       status = true;
       msg = "Record successfully added!";
   }
   return status;
}

bool database::updateRecord(record myRecord)

{
   bool status;
   bool done = false;

   if (storage.empty()) //checking if DB is empty
   {
      
       status = false;
       msg = "Record not found!";
       done = true;
   }

   //checking to see if record exists
   if (!done)
   {
       unsigned int i;
       for (i = 0; (i < (storage.size())) && (myRecord.idField > storage[i].idField); ++i);
       if (i < (storage.size()) && myRecord.idField == storage[i].idField)
       {
           status = true;
           storage[i] = myRecord;
           msg = "Record successfully updated!";
           index = 0;
       }
       else
       {
           status = false;
           msg = "No match found.";
           //status = true; not true till element/record is inserted
       }
   }

   return status;

}

bool database::deleteRecord(record myRecord)
{

   bool status;
   bool done = false;

   if (storage.empty()) //checking if DB is empty
   {

       status = false;
       msg = "Record not found!";
       done = true;
   }

   //checking to see if record exists
   if (!done)
   {
       unsigned int i;
       for (i = 0; (i < (storage.size())) && (myRecord.idField > storage[i].idField); ++i);
       if (i < (storage.size()) && myRecord.idField == storage[i].idField)
       {
           status = true;
           for (; i < storage.size() - 1; ++i) // nothing in front of first ; in for loop because of previous for loop
           {
               storage[i] = storage[i + 1]; //moves records accordingly during deleting

           }
           storage.pop_back(); //gives memory back after deleting record

           msg = "Record successfully deleted!";

           index = 0;

           if (storage.size() == 0)
               index = -1;
       }
       else
       {
           status = false;
           msg = "No match found.";
           //status = true; not true till element/record is inserted
       }
   }

   return status;
}

//viewRecord print out for main function
bool database::resetIndex()
{
   bool status; //this bool status only applies in this section

   if (storage.size() == 0)
   {
       index = -1;
       status = false;
       msg = "Database is empty. Index cannot be reset.";
   }
   else
   {
       index = 0;
       status = true;
       msg = "Index successfully reset.";

   }
   return status;
}

bool database::incrIndex()
{
   bool status; //this bool status only applies in this section

   if (storage.size() == 0)
   {
       index = -1;
       status = false;
       msg = "Database is empty. Index cannot be incremented.";
   }
   else if (storage.size()-1 == index)
   {
       status = false;
       msg = "End of database reached. Could not increment index.";
   }
   else
   {
       ++index;
       status = true;
       msg = "Index successfully incremented.";

   }
   return status;

}

bool database::decrIndex()
{
   bool status; //this bool status only applies in this section

   if (storage.size() == 0)
   {
       index = -1;
       status = false;
       msg = "Database is empty. Index cannot be decremented.";
   }
   else if (index == 0)
   {
       status = false;
       msg = "Could not decrement index.";
   }
   else
   {
       --index;
       status = true;
       msg = "Index successfully decremented.";

   }
   return status;

}

record database::getRecord()
{
   record myRecord;
   if (storage.empty() && index == -1)
   {
       msg = "Database empty.";
       myRecord.idField = -1;
   }
   else
   {
       msg = "Record found!";
       myRecord = storage[index];
   }


   return myRecord;

}
//if index is at the end, how can a record be retrieved...find solution

bool database::findRecord(record myRecord)
{
   bool status;
   bool done = false;

   if (storage.empty()) //checking if DB is empty
   {

       status = false;
       msg = "Record not found!";
       done = true;
   }

   //checking to see if record exists
   if (!done)
   {
       unsigned int i;
       for (i = 0; (i < (storage.size())) && (myRecord.idField > storage[i].idField); ++i);
       if (i < (storage.size()) && myRecord.idField == storage[i].idField)
       {
           status = true;
           index = i;
           msg = "Record successfully found!";
       }
       else
       {
           status = false;
           msg = "No match found.";
       }
   }

   return status;

}

string database::getmsg()
{
   return msg;

}

***********

Explanation / Answer

In the above mentioned we need to add item,update item and delete item.

Case A:

Cout<< provide the information need to add item to database<<””;

Cout<< “enter id”;

Cin>>myrecord.id field;

Cout<<”enter L name”;

Cin>>myrecord.Lname;

Cout<<”entr F name”;

Cin>>myrecord.F name;

cout << “enter satScore”;

cin >> myRecord.satScore

cout << “enter gender" ;

cin >> myRecord.gender ;

cout << “enter GPA " ;

cin>> myRecord.GPA ;

done=mydatabase.addrecord(myrecord);

Like that we need to upadate fileds

then if we want to delete simply we delete the id no.then the other records corresponding to that id will delete.

Note:if you need further explanation please let me know.

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