C++ Program Program creating book-store database. Each command requires separate
ID: 3887941 • Letter: C
Question
C++ Program
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
struct Book {
string ISBN;
string title;
int quantity;
int edition;
double price;
};
void swap(struct Book &x, struct Book &y){
struct Book temp;
temp.ISBN = x.ISBN;
temp.title = x.title;
temp.quantity = x.quantity;
temp.edition = x.edition;
temp.price = x.price;
x.ISBN = y.ISBN;
x.title = y.title;
x.quantity = y.quantity;
x.edition = y.edition;
x.price = y.price;
y.ISBN = temp.ISBN;
y.title = temp.title;
y.quantity = temp.quantity;
y.edition = temp.edition;
y.price = temp.price;
}
int main(){
vector<Book> data;
ifstream fin;
string input;
string isbn;
int quantity;
int edition;
int found;
double price;
string title;
struct Book item;
fin.open("storeTrans.txt");
if (fin){
while(fin >> input ){
if (input == "AddBook"){
fin >> item.ISBN >> item.title >> item.quantity >> item.price >> item.edition;
found = 0;
for(int i = 0; i<data.size(); i++){
if (data[i].ISBN == isbn){
found = 1;
quantity = data[i].quantity;
}
}
if (found == 0){
data.push_back(item);
cout << isbn << " successfully added to the database" << endl;
}
else {
cout << isbn << " already exists. The number of copies is " << quantity << endl;
}
}
if (input == "SearchBook"){
fin >> title;
int found = 0;
for(int i = 0; i<data.size(); i++){
if (data[i].ISBN == isbn){
found = 1;
cout << data[i].ISBN << " "<< title << endl;
}
}
if (found == 0)
cout << title << " book does not exist " << endl;
}
if (input == "DeleteBook"){
fin >> isbn;
int found = 0;
for(int i = 0; i<data.size(); i++){
if (data[i].ISBN == isbn){
found = 1;
data.erase(data.begin()+i);
}
}
if (found == 1)
cout << isbn << " book successfully deleted "<< endl;
else
cout << isbn << " book does not exist " << endl;
}
if (input == "UpdatePrice"){
fin >> isbn >> price;
int found = 0;
for(int i = 0; i<data.size(); i++){
if (data[i].ISBN == isbn){
found = 1;
data[i].price = price;
}
}
if (found == 1)
cout << isbn << " book price updated to " << quantity << endl;
else
cout << isbn << " book does not exist " << endl;
}
if (input == "UpdateQuantity"){
fin >> isbn >> quantity;
int found = 0;
for(int i = 0; i<data.size(); i++){
if (data[i].ISBN == isbn){
found = 1;
data[i].quantity = quantity;
}
}
if (found == 1)
cout << isbn << " book quantity updated to " << quantity << endl;
else
cout << isbn << " book does not exist " << endl;
}
if (input == "Sort"){
for(int i = 0; i<data.size(); i++){
for(int j = i; j<data.size(); j++){
if (data[i].title > data[j].title){
swap(data[i],data[j]);
}
if (data[i].title == data[j].title){
if (data[i].edition > data[j].edition)
swap(data[i],data[j]);
}
}
}
cout << "Database Sorted.." << endl;
}
if (input == "Save"){
ofstream fout("storeOut.txt");
for(int i = 0; i<data.size(); i++){
fout << data[i].ISBN << " " << data[i].title << " " << data[i].edition;
fout << " " << data[i].quantity << " " << data[i].price << endl;
}
fout.close();
}
}
}
else {
cout << "Error opening file ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.