he goal of this exercise is for your to create your own database. I am purposely
ID: 3829148 • Letter: H
Question
he goal of this exercise is for your to create your own database. I am purposely leaving this as open ended as possible so you are free to be creative.
Create a struct of your choosing that has at least two different data types (ie string, int, etc). This struct will represent one "record" in your database.
Create a menu loop that gives the following options:
Print the contents of the database. Make the formatting look decent (you can use <iomanip>).
Append a new record. This will prompt the user to enter the data, then append the struct values to the end of the file.
Search for a record by variable 1. Print out the contents of the record if found. Notify if not found.
Search for a record by variable 2. Print out the contents of the record if found. Notify if not found.
Sort the records by variable 1 and write the contents to the file.
Sort the records by variable 2 and write the contents to the file.
Exit.
Be sure that any changes to the database, ie sorting and/or appending, are written to the database file so they persist after the program is closed. I will be testing this.
A few things to remember:
It may be a simpler design to open the file for a specific purpose, ie in, out, or append, depending on the operation, and then close the file when the operation is complete.
You won't be able to use binary search if the data is not sorted.
If you want to keep the file constantly open, which will be harder to code, you can:
Open an fstream for both reading and writing if you use ios::in | ios::out.
If the file does not already exist, the open will fail with ios::in|ios::out
If the file open fails, you should open with just ios::out so it creates a new file
You can seek to a different byte location in the file using seekg for an ifstream, seekf for an ofstream, and either one for an fstream.
If you use ios::app, every write will go to the end of the file, regardless of whether or not you use seek.
Please add <fstream> and a sorting algorithm to the following code.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
string mystr;
mine.title = "2001 A Space Odyssey";
mine.year = 1968;
cout << "Enter title: ";
getline (cin,yours.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> yours.year;
cout << "My favorite movie is: ";
printmovie (mine);
cout << "And yours is: ";
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ") ";
}
Explanation / Answer
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
string mystr;
mine.title = "2000 A Space Odyssey";
mine.year = 1998;
cout << "Enter title: ";
getline (cin,yours.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> yours.year;
cout << "My favorite movie is: ";
printmovie (mine);
cout << "And yours is: ";
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ") ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.