Welcome to Westonci.ca, where curiosity meets expertise. Ask any question and receive fast, accurate answers from our knowledgeable community. Experience the convenience of getting accurate answers to your questions from a dedicated community of professionals. Join our Q&A platform to connect with experts dedicated to providing accurate answers to your questions in various fields.
Sagot :
Use the knowledge in computational language in C++ to write the a code with alphabetical order of author name.
How to define an array in C++?
An Array is a set of values arranged in lists and accessible through a positive numeric index. So, we have that each position of our Array is a variable of the type of our Array, so we have to have a way to initialize this set of variables.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct Book {
string title;
string author;
};
const int ARRAY_SIZE = 1000;
Book books [ARRAY_SIZE];
string pathname;
ifstream library;
int LoadData();
void ShowAll(int count);
void ShowBooksByAuthor(int count, string name);
void ShowBooksByTitle(int count, string title);
void sortByTitle(int count, string title);
void sortByAuthor(int count, string author);
int main()
{
int count = 0;
char selector = 'q', yesNoAnswer = 'n';
string name;
string title;
cout << "Welcome to Forrest's Library Database." << endl;
cout << "Please enter the name of the backup file: ";
getline(cin, pathname);
LoadData();
count = LoadData();
cout << count << " records loaded successfully." << endl;
do
{
cout << endl << "\t(S)how All, Search (A)uthor, Search (T)itle, (Q)uit: ";
cin >> selector;
selector = toupper(selector);
switch(selector)
{
case 'S':
sortByTitle(count, title);
if (count <= 0)
cout << "No counts found!\n";
else
ShowAll(count);
break;
case 'A':
sortByAuthor(count, name);
cout << "bookAuthor: ";
cin.ignore();
getline(cin, name);
if (count <= 0)
cout << "No records found!\n";
else
ShowBooksByAuthor(count, name);
break;
case 'T':
sortByTitle(count, title);
cout << "bookTitle: ";
cin.ignore();
getline(cin, title);
if (count <= 0)
cout << "No records found!\n";
else
ShowBooksByTitle(count, title);
break;
}
}
while (selector != 'q' && selector != 'Q');
return 0;
}
int LoadData()
{
int count = 0;
int i = 0;
library.open(pathname);
ifstream library(pathname);
if (!library)
{
cout << "Cannot open backup file" << endl;
return 0;
}
while (!library.eof())
{
getline(library, books[count].title);
getline(library, books[count].author);
count++;
}
return count;
}
void ShowAll(int count)
{
for (int i = 0; i < count; i++)
{
cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
}
}
void ShowBooksByAuthor(int count, string name)
{
int j = 0;
for (int i = 0; i < count; i++)
{
if(books[i].author.find(name) < 100)
{
cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
j++;
}
}
cout << j << " records found";}
void ShowBooksByTitle(int count, string title)
{
int j = 0;
for (int i = 0; i < count; i++)
{
if(books[i].title.find(title) < 100)
{
cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
j++;
}
}
cout << j << " records found";
}
void sortByTitle(int count, string title) {
string temp;
for (int i = 0; i < count; i++) {
for(int j = 0; j < count - i; j++) {
if (books[j].title > books[j + 1].title) {
temp = books[j].title;
books[j].title = books[j + 1].title;
books[j + 1].title = temp;
}
}
}
}
void sortByAuthor(int count, string name) {
string temp;
for (int i = 0; i < count; i++) {
for(int j = 0; j < count - i; j++) {
if (books[j].author > books[j + 1].author) {
temp = books[j].author;
books[j].author = books[j + 1].author;
books[j + 1].author = temp;
}
}
}
}
See more about C++ at brainly.com/question/19705654
Thank you for trusting us with your questions. We're here to help you find accurate answers quickly and efficiently. We appreciate your visit. Our platform is always here to offer accurate and reliable answers. Return anytime. We're here to help at Westonci.ca. Keep visiting for the best answers to your questions.