FreePlay Acade in Richardson has a primitive flat file database that needs to be
ID: 3665089 • Letter: F
Question
FreePlay Acade in Richardson has a primitive flat file database that needs to be updated. This database contains the names, high scores (w/ initials) and the total number of plays of their games. The database also contains the total amount each game would earn if not on free play. You will build a simple interface to interact with the database. Details: The interface will provide the following options: 1. Add a record to the database 2. Search for a record and display it 3. Edit a record 4. Delete a record Add a record: Each record should be added to the end of the file. Search for a record: The search term will be a word or phrase. Search through the file and display the complete record for any game that matches the search term. Edit a record: User will enter a game name. The program should display a menu for the user to select which part of the record to edit. Once the user has made a selection, ask for the appropriate piece of information. The program should update the file and confirm the change by displaying the new record on the screen. The user can edit the following items: 1. High score 2. Initials 3. Number of plays If number of plays is edited, the revenue should be recalculated Delete a record: User will enter a game name. The program should delete the record from the file. The best way to do this is to copy all the data except the record to delete into a new file, delete the old file and then rename the new file. Input will come from a batch file. Output will be written to a file. You will need to use the rename and remove functions for this assignment. Both of these functions are located in stdio.h. o http://www.cplusplus.com/reference/cstdio/remove/ o http://www.cplusplus.com/reference/cstdio/rename/ o You must convert the string holding the filename to a C-string in order for the functions to work (stringvar.c_str()). Database Format: The database file will be named freeplay.dat. Each record in the database will be on a single line with the following format. Each field will be separated by a comma and a space , , , , <$>< > - may be multiple words - 9 digits - 3 characters – no white space - 4 digits – will have leading zeroes if value less than 1000 - <4 digits><2 digits> - will have leading zeroes if first four digits less than 1000 Input: All input will be file based. You will create a batch file to use with the database. The format for each option is listed below: Add a record o 1 ”name” high_score initials plays $revenue< > Search for a record o 2 o Search term may contain spaces Edit a record o 3 o 1 = high score 2 = initials 3 = number of plays o New value may not contain leading zeroes Delete a record o 4 o Name may contain spaces Output: Each command in the input file will generate output to a file named freeplay.log. The output for each command is as follows: Add a record o RECORD ADDED Name: High Score: Initials: Plays: Revenue: $ - formatted to 2 decimal places Search for a record o FOUND or NOT FOUND o If found High Score: Initials: Plays: Revenue: $ - formatted to 2 decimal places Edit a record o UPDATED o UPDATE TO - VALUE o Name: High Score: Initials: Plays: Revenue: $ - formatted to 2 decimal places Delete a record o RECORD DELETED Name: High Score: Initials: Plays: Revenue: $ - formatted to 2 decimal places
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
string filename="freeplay.dat";
/*Each record should be added to the end of the file.*/
void addRecord()
{
cout<<endl<<"Enter Name:";
string name;
cin>>name;
cout<<endl<<"Enter high score:";
double highScore;
cin>>highScore;
cout<<endl<<"Enter Initials:";
string initials;
cin>>initials;
cout<<endl<<"Enter plays:";
int plays;
cin>>plays;
cout<<endl<<"Enter Revenue:";
int revenue;
cin>>revenue;
//open the file in append mode
std::fstream fs;
fs.open (filename.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
//append the new record
fs<<endl<<name<<" "<<highScore<<" "<<initials<<" "<<plays<<" "<<revenue;
cout<<endl<<"RECORD ADDED";
fs.close();
}
/*The search term will be a word or phrase. Search through
the file and display the complete record for any game that
matches the search term.*/
void searchAndDisplay()
{
string gameName;
cout<<endl<<"Enter the game name to search: ";
cout<<gameName;
ifstream infile(filename.c_str());
string name;
double highScore;
string initials;
int plays;
double revenue;
int flag=-1;
while(!infile.eof())
{
infile>>name>>highScore>>initials>>plays>>revenue;
if(gameName.compare(name)==0)
{
flag=1;
cout<<endl<<"Record Found";
cout<<name<<" "<<highScore<<" "<<initials<<" "<<plays<<" "<<revenue;
break;
}
}
if(flag==-1)
cout<<endl<<"Record not found";
}
/* User will enter a game name. The program should display a menu
for the user to select which part of the record to edit. Once the
user has made a selection, ask for the appropriate piece of
information. The program should update the file and confirm
the change by displaying the new record on the screen.
The user can edit the following items: 1. High score
2. Initials 3. Number of plays ? If number of plays is edited,
the revenue should be recalculated ?*/
void editRecord()
{
string gameName;
cout<<endl<<"Enter the game name: ";
cout<<gameName;
ifstream infile(filename.c_str());
int flag=-1;
string name;
double highScore;
string initials;
int plays;
double revenue;
while(!infile.eof())
{
infile>>name>>highScore>>initials>>plays>>revenue;
if(gameName.compare(name)==0)
{
flag=1;
cout<<endl<<"Record Found";
cout<<name<<" "<<highScore<<" "<<initials<<" "<<plays<<" "<<revenue;
break;
}
}
if(flag==-1)
cout<<endl<<"Record not found";
else
{
ifstream fs(filename.c_str());
ofstream temp("temp.txt");
while(!fs.eof())
{
fs>>name>>highScore>>initials>>plays>>revenue;
if(gameName.compare(name)!=0)
{
temp<<name<<" "<<highScore<<" "<<initials<<" "<<plays<<" "<<revenue;
}
else
{
int option;
cout<<endl<<"Select which part of the record to edit:";
cout<<endl<<"1. High score";
cout<<endl<<"2. Initials";
cout<<endl<<"3. Number of plays";
cin>>option;
double newhighScore;
string newinitials;
int newplays;
double newrevenue;
if(option==1)
{
cout<<endl<<"Enter new high score: ";
cin>>newhighScore;
temp<<name<<" "<<newhighScore<<" "<<initials<<" "<<plays<<" "<<revenue;
}
else if(option==2)
{
cout<<endl<<"Enter new initials: ";
cin>>newinitials;
temp<<name<<" "<<newhighScore<<" "<<newinitials<<" "<<plays<<" "<<revenue;
}
else if(option==3)
{
cout<<endl<<"Enter new number of plays: ";
cin>>newplays;
//calculate new revenue and update it
newrevenue=revenue;
temp<<name<<" "<<newhighScore<<" "<<initials<<" "<<newplays<<" "<<revenue;
}
}
}
fs.clear(); // clear eof and fail bits
fs.seekg(0, ios::beg);
fs.close();
temp.close();
remove(filename.c_str());
rename("temp.txt",filename.c_str());
cout<<endl<<"Record updated successfully";
}
}
/*User will enter a game name. The program should delete the
record from the file.*/
void deleteRecord()
{
string gameName;
cout<<endl<<"Enter the game name to search: ";
cout<<gameName;
ifstream infile(filename.c_str());
string name;
double highScore;
string initials;
int plays;
double revenue;
int flag=-1;
while(!infile.eof())
{
infile>>name>>highScore>>initials>>plays>>revenue;
if(gameName.compare(name)==0)
{
flag=1;
cout<<endl<<"Record Found";
break;
}
}
infile.close();
if(flag==-1)
cout<<endl<<"Record not found";
else
{
string name;
double highScore;
string initials;
int plays;
double revenue;
ifstream fs(filename.c_str());
ofstream temp("temp.txt");
while(!fs.eof())
{
infile>>name>>highScore>>initials>>plays>>revenue;
if(gameName.compare(name)!=0)
{
temp<<name<<" "<<highScore<<" "<<initials<<" "<<plays<<" "<<revenue;
}
}
fs.clear(); // clear eof and fail bits
fs.seekg(0, ios::beg);
fs.close();
temp.close();
remove(filename.c_str());
rename("temp.txt",filename.c_str());
cout<<endl<<"Record deleted successfully";
}
}
int main()
{
cout<<endl<<"1. Add a record to the database";
cout<<endl<<"2. Search for a record and Display";
cout<<endl<<"3. Edit a record";
cout<<endl<<"4. Delete a record";
cout<<endl<<"5. Quit";
cout<<endl<<"Enter option: ";
int option;
cin>>option;
switch(option)
{
case 1: addRecord();
break;
case 2: searchAndDisplay();
break;
case 3: editRecord();
break;
case 4: deleteRecord();
break;
case 5: exit(0);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.