1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
motikmotik
2 years ago
5

Develop a program to sort a file consisting of bonks details in the alphabetical order of author names. The details of books inc

lude book id, author_name, price. no of pages, publisher, year of publishing
Please provide C++ program for above question.​
Computers and Technology
1 answer:
olga2289 [7]2 years ago
6 0

Use the knowledge in computational language in C++ to write the a code with alphabetical order of author name.

<h3>How to define an array in C++?</h3>

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.

<em>#include <iostream></em>

<em>#include <iomanip></em>

<em>#include <fstream></em>

<em>#include <string></em>

<em>using namespace std;</em>

<em>struct Book {</em>

<em>    string title;</em>

<em>    string author;</em>

<em>};</em>

<em>const int ARRAY_SIZE = 1000;</em>

<em>Book books [ARRAY_SIZE];</em>

<em>string pathname;</em>

<em>ifstream library;</em>

<em>int LoadData();</em>

<em>void ShowAll(int count);</em>

<em>void ShowBooksByAuthor(int count, string name);</em>

<em>void ShowBooksByTitle(int count, string title);</em>

<em>void sortByTitle(int count, string title);</em>

<em>void sortByAuthor(int count, string author);</em>

<em>int main()</em>

<em>{   </em>

<em>    int count = 0;</em>

<em>    char selector = 'q', yesNoAnswer = 'n';</em>

<em>    string name;</em>

<em>    string title;</em>

<em>      cout << "Welcome to Forrest's Library Database." << endl;</em>

<em>    cout << "Please enter the name of the backup file: ";</em>

<em>    getline(cin, pathname);</em>

<em>    LoadData();</em>

<em>    count = LoadData();</em>

<em>    cout  << count << " records loaded successfully." << endl;</em>

<em>    do </em>

<em>    {</em>

<em>        cout << endl << "\t(S)how All, Search (A)uthor, Search (T)itle, (Q)uit: "; </em>

<em>        cin >> selector;</em>

<em>        selector = toupper(selector);</em>

<em>        switch(selector)</em>

<em>        {</em>

<em>            case 'S': </em>

<em>                sortByTitle(count, title);</em>

<em>                if (count <= 0)</em>

<em>                    cout << "No counts found!\n";</em>

<em>                else</em>

<em>                    ShowAll(count);</em>

<em>                break;</em>

<em>            case 'A': </em>

<em>                sortByAuthor(count, name);</em>

<em>                cout << "bookAuthor: ";</em>

<em>                cin.ignore();</em>

<em>                getline(cin, name);</em>

<em>                if (count <= 0)</em>

<em>                    cout << "No records found!\n";</em>

<em>                else</em>

<em>                    ShowBooksByAuthor(count, name); </em>

<em>                break;</em>

<em>            case 'T': </em>

<em>                sortByTitle(count, title);</em>

<em>                cout << "bookTitle: ";</em>

<em>                cin.ignore();</em>

<em>                getline(cin, title);</em>

<em>                if (count <= 0)</em>

<em>                    cout << "No records found!\n";</em>

<em>                else</em>

<em>                    ShowBooksByTitle(count, title);      </em>

<em>                break; </em>

<em>        }</em>

<em>    }</em>

<em>    while (selector != 'q' && selector != 'Q'); </em>

<em>    return 0;</em>

<em>}</em>

<em>int LoadData() </em>

<em>{</em>

<em>    int count = 0;</em>

<em>    int i = 0;</em>

<em>    library.open(pathname);</em>

<em>    ifstream library(pathname);</em>

<em>    if (!library)</em>

<em>    {</em>

<em>        cout << "Cannot open backup file" << endl;</em>

<em>        return 0;</em>

<em>    }</em>

<em>    while (!library.eof())</em>

<em>    {</em>

<em>        getline(library, books[count].title);</em>

<em>        getline(library, books[count].author);</em>

<em>        count++;</em>

<em>    }</em>

<em>    return count;</em>

<em>}</em>

<em>void ShowAll(int count) </em>

<em>{</em>

<em>    for (int i = 0; i < count; i++)</em>

<em>    {</em>

<em>       cout << books[i].title << " " << "(" << books[i].author << ")" << endl;</em>

<em>    }</em>

<em>}</em>

<em>void ShowBooksByAuthor(int count, string name) </em>

<em>{</em>

<em>    int j = 0;</em>

<em>    for (int i = 0; i < count; i++)</em>

<em>    {</em>

<em>        if(books[i].author.find(name) < 100) </em>

<em>        {</em>

<em>            cout << books[i].title << " " << "(" << books[i].author << ")" << endl;</em>

<em>            j++;</em>

<em>        }</em>

<em>    }</em>

<em>    cout << j << " records found";}</em>

<em>void ShowBooksByTitle(int count, string title) </em>

<em>{</em>

<em>    int j = 0;</em>

<em>    for (int i = 0; i < count; i++)</em>

<em>    {</em>

<em>        if(books[i].title.find(title) < 100)</em>

<em>        {</em>

<em>            cout << books[i].title << " " << "(" << books[i].author << ")" << endl;</em>

<em>            j++;</em>

<em>        }</em>

<em>    }</em>

<em>    cout << j << " records found";</em>

<em>}</em>

<em>void sortByTitle(int count, string title) {</em>

<em>    string temp;</em>

<em>    for (int i = 0; i < count; i++) {</em>

<em>        for(int j = 0; j < count - i; j++) {</em>

<em>            if (books[j].title > books[j + 1].title) {</em>

<em>                temp = books[j].title;</em>

<em>                books[j].title = books[j + 1].title;</em>

<em>                books[j + 1].title = temp;</em>

<em>            }</em>

<em>        }</em>

<em>    }</em>

<em>}</em>

<em>void sortByAuthor(int count, string name) {</em>

<em>    string temp;</em>

<em>    for (int i = 0; i < count; i++) {</em>

<em>        for(int j = 0; j < count - i; j++) {</em>

<em>            if (books[j].author > books[j + 1].author) {</em>

<em>                temp = books[j].author;</em>

<em>                books[j].author = books[j + 1].author;</em>

<em>                books[j + 1].author = temp;</em>

<em>            }</em>

<em>        }</em>

<em>    }</em>

<em>}</em>

See more about C++ at brainly.com/question/19705654

You might be interested in
What is the decimal form of the following binary ip address? 11001101.00111001.10101001.01000010
jasenka [17]

The decimal form of the 11001101.00111001.10101001.01000010 binary ip address is 205.57.169.66.

An IP address is a binary number with 32 bits. The 32 bits are divided into four octets, which are groupings of 8 bits each. An IP address, however, is shown as a dotted decimal number (for example: 205.57. 32.9).

Memory regions are given binary addresses by the computer system. But in order to access a memory location, the system utilises a certain number of bits. We can address two memory regions with 1 bit. We can address 4 memory locations with 2 bits and 8 memory locations with 3 bits.

The 4 sets of 8 bits in each of the 4 directions are simply written down to create the 32-bit binary IP address.

Learn more about binary ip address:

brainly.com/question/14541891

#SPJ4

3 0
2 years ago
A mismatch between the type of printer connected to a system and the software printer driver installed is an example of a ____.
mixer [17]
ERROR! It is an example of an error
6 0
3 years ago
Which type of processing best describes the correction of errors or the rearrangement of a job's running time?
Neko [114]

Answer:

Real-time

Explanation:

i am sure because on my test it said corect

6 0
3 years ago
Disabling cookies prevents you from being the victim of a virus <br> A.True B.False
wariber [46]

The correct answer is True

3 0
3 years ago
Read 2 more answers
The penalties for ignoring the requirements for protecting classified information when using social networking services are ____
Sloan [31]

Answer: the same as

Explanation: Sensitive information are usually tagged as classified and as such only individuals with the necessary security clearance have access to information of these sort. The sensitivity of these information stems from the damage that could ensue from it's leakage. Those found culpable could be sued for various degree of criminal offenses. The rules binding the rules, regulation and protection of these sensitive information is similar to those binding on other dissemination methods. This may be due to the degree of chaos and it's leakage through any channel of dissemination could be damaging. Penalties if found guilty may range from imprisonment, demotion, termination of appointment and so on.

3 0
2 years ago
Other questions:
  • La inteligencia o unidad lógica de un computador se denomina como??? Software, Hardware CPU o que, ayuda.....
    7·1 answer
  • A directional antenna issues wireless signals along a(n) ____ direction.
    9·1 answer
  • Two cars A and B leave an intersection at the same time. Car A travels west at an average speed of x miles per hour and car B tr
    6·1 answer
  • Simplify the expresion<br>12. 12g + 9g​
    13·1 answer
  • What type of virus was the "lloveyou" virus?<br> 1.Trojan<br> 2.Worm
    10·2 answers
  • What is your favorite LEGO set
    5·2 answers
  • In Outlook 2016, the Tell Me function can be accessed by
    15·2 answers
  • Copying materials from a source text without using is<br> considered plagiarism<br> ?
    5·1 answer
  • Write a program which will -
    14·1 answer
  • The ________ is the biggest power consumer on a mobile computing device.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!