. For this lab, you will write a program to manage a simple database. This datab
ID: 3839185 • Letter: #
Question
.
For this lab, you will write a program to manage a simple database. This database will store Records (from your previous lab) stored as a Linked List (code provided).
You will design an efficient text-based menu to allow users to
add a new record,
delete a record,
search for an existing record, and
print out the records.
User Menu
Create a new file Project5.cpp that will contain your main() function. Create a new instance of LinkedList. This will create an (initially) empty list. Next, create a text-based user menu. The menu should repeat until the user chooses to quit. Your menu will provide the following options:
Insert
Delete
Search
Quit
Insert an element - create a new record, populate it with user provided data (i.e., call input() function), and add it to the linked list.
Delete an element - prompt the user to enter an ID number. Delete the record from the linked list.
Search for an element - prompt the user to enter an ID number. Search the list for the element. If found, print it. Otherwise, print a message that the item was not found.
Print out the database (all records) - iterate over the linked list and print out each record one by one (i.e., call your display() function). You will need to create a new function printAll() in the class Linked List (see below).
Quit - exits your program.
Linked List
In this lab, you will make use of a Data Structure called a Linked List. This is an important data structure you will implement in 211. For now, I have provide you most all of the code you will need to use a Linked List. In the version I give you, the list maintains a sorted list of Records, based on the Record ID. I have linked a short video below that explains how linked list work.
Download the provided .cpp and .h files. Study the provided files until you understand how the linked list works. Notice I have provided functions to insert, remove, and search the list. I also provide a function print(), which prints out in order the IDs in the list. Add another function to the LinkedList class called printRecords(). This function will be very similar to print(), except instead of the printing only the IDs it will print out the entire Record (i.e., calls your display() function).
Makefile
Write a Makefile to compile all of your Project5 code files.
Text Input File
In order to help test your file, create a file called "input.txt" that contains text input to your program.
You must be able to run it as
./project5 < input.txt
In the above line, we are using unix to "pipe" in data from the file directly into standard in (cin). Do not use file streams (fstreams).
In this file, you will provide the input for FIVE records. That means you will choose menu option "1" (input) and provide the values for a new Record field by field (in proper order). Then repeat this for four more records.
For example, a file for a Book might look like the following:
1 // Menu option to choose "Insert"
100 // Answer to prompt for ID
Fahrenheit 451 // Answer to prompt for Name
Ray Bradbury // .....................Author
1953 // .....................Year
153 // .....................Number of pages
Ballantine Books // .....................Publisher
978-0-7432-4722-1 // .....................ISBN
1 // Menu option to choose "Insert"
// continues for four more records
5 // Exit menu
Note: the C++ style comments provided for explanation only and would not be included in your file.
Make sure to test your input.txt file. If you get a repeating menu, add the command to exit (5) at the end.
---------------------------------------------------------------------------
This is the LinkedList.cpp file:
Explanation / Answer
Analyzing your problem statement you have done almost correct.
Please find the remaining code for reading the text file and the other necessary operations
***********************
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct cdStruct //Structure containing Cd Database Elements
{
string name;
string author;
int year;
int numberofpages;
int ISBN;
cdStruct *next;
};
void save(cdStruct *ptr) //Function that saves database info to file
{
ofstream dataFile;
dataFile.open("output.txt", ios::out | ios::app);
dataFile << ptr->name << endl;
dataFile << ptr->author << endl;
dataFile << ptr->year << endl;
dataFile << ptr->numberOfpages << endl;
dataFile << ptr->ISBN << endl;
dataFile.close();
}
void saveStructList(cdStruct *start) //Function that passes database info to the save function
{
ofstream dataFile;
dataFile.open("output.txt",ios::trunc);
dataFile.close();
cdStruct *ptr = start->next;
while(ptr)
{
save(ptr);
ptr = ptr->next;
}
}
//FUNCTION THAT READS THE DATA FROM THE TEXT FILE
void addLoadEntry(cdStruct* start) //Function that creates a new structure
{
fstream dataFile("output.txt", ios::in);
//while(!dataFile.eof());
{
cdStruct* newStruct = new cdStruct;
cdStruct *newEntry = newStruct; //getnewstruct()
getline(dataFile, newStruct->name);
getline(dataFile, newStruct->author);
dataFile >> newStruct->year;
dataFile >> newStruct->numberofpages;
dataFile >> newStruct->ISBN;
cdStruct *ptr = start;
while(ptr->next) // while the current entry points to ANOTHER entry
ptr = ptr->next;
// at this point ptr is a valid entry that points to null so it is the end (or tail) of the list
ptr->next = newEntry;
ptr->next->next = NULL;
}
dataFile.close();
}
void printStruct(cdStruct *ptr) //Function that prints database
{
cout << "Name: " << ptr->name << endl;
cout << "Author: " << ptr->author << endl;
cout << "Year: " << ptr->year << endl;
cout << "Number of pages: " << ptr->numberOfSongs << endl;
cout << "ISBN" << ptr->ISBN << endl
}
void printStructList(cdStruct *start) //Function that sends database information to print function
{
cdStruct *ptr = start->next;
while(ptr)
{
printStruct(ptr);
ptr = ptr->next;
}
}
cdStruct *getNewStuct() //Function to dynamically create a new structure as needed
{
cdStruct* newStruct = new cdStruct;
cout << "Enter Name: ";
getline(cin, newStruct->name);
cout<< "Enter AuthorName: ";
getline(cin, newStruct->author);
cout << "Enter year ";
getline(cin, newStruct->year);
cout << "Enter Number of pages";
getline(cin,newStruct->numberofpages);
cout << "Enter ISBN";
getline(cin,newStruct->ISBN);
return newStruct;
}
void addEntry(cdStruct* start) //Function that creates a new structure
{
cdStruct *newEntry = getNewStuct();
cdStruct *ptr = start;
while(ptr->next) // while the current entry points to ANOTHER entry
ptr = ptr->next;
// at this point ptr is a valid entry that points to null so it is the end (or tail) of the list
ptr->next = newEntry;
ptr->next->next = NULL;
}
int main()
{
cdStruct *start = new cdStruct; // first element will NOT handle any info
start->next = NULL;
int temp, userChoice;
do
{
// print menu
cout << "------- Super CD 2.0 --------" << endl << endl;
cout << "1. Print Database" << endl;
cout << "2. Create a New Entry" << endl;
cout << "4. Save Database" << endl;
cout << "5. Load Database" << endl;
cout << "6. Exit " << endl << endl;
cout << "Your Selection -> ";
cin >> userChoice;
cin.ignore(200,' ');
switch(userChoice)
{
case 1: //print user mendu
printStructList(start);
break;
case 2: //allow user to make a new entry
addEntry(start);
break;
case 4: //allow user to save the database to file
saveStructList(start);
break;
case 5: //allow user to load an existing database
addLoadEntry(start);
break;
case 6: //allow user to exit the program
cout << endl << "You Chose to Exit" << endl;
} // switch
}while(userChoice != 6);
return 0;
}
*********************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.