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
Which design principle is the subject of a photo or image? ​
VashaNatasha [74]

The focal point is where the eye is drawn to in a photo or image.

6 0
3 years ago
What is the output produced from the following statements? System.out.println("\"Quotes\""); System.out.println("Slashes \\//");
jekas [21]

Answer:

"Quotes"

Slashes \//

How '"confounding' "\" it is!

Explanation:

The question above is testing your knowledge of  the "\" escape sequence, This escape sequence is used to introduce special formatting to the output of the System.out.print function in Java.

It can be used to introduce a new line \n

It can also be used to introduce a tab indentation \t

As in the question above it is used to introduce double quotes "" in this case \"

Also as we see the question above it can still be used to place backlashes to an output in this case we use two backlashes \\. The first is the escape sequence, the second \ gets printed out.

3 0
3 years ago
If several programs or apps are running simultaneously, your computer or device might use up its
kirill [66]

Answer: I am pretty sure it's RAM

Explanation:

I'm not 100% positive bc I haven't gotten my scores back yet but I think that's it.

7 0
2 years ago
Which tab provides commands for the most commonly used elements in Word Software?
Flauer [41]
I think it should be in the design tab
6 0
3 years ago
Role of computers in everyday life​
Simora [160]

Answer:

In modern word everything around us like GPS, ATM machines, cell phones, petrol pumps, portable play stations and all other modern devices use computer controlling units to conduct their featured operations. Surely computers have very elaborated role in daily day life of humans.

Explanation:

hope it helps thxs

8 0
3 years ago
Other questions:
  • Using functions,
    6·1 answer
  • How would you say an hard drive works
    9·2 answers
  • Stealing passwords by using software code to run through various password schemes with numbers, symbols, capital letters, and ch
    6·1 answer
  • Suppose Host A wants to send a large file to Host B. The path from Host A to Host B has three links, of rates R1 = 500 kbps, R2
    14·1 answer
  • Which part of a touchscreen responds to pressure applied to its surface?
    6·1 answer
  • What do you find when you first open a word processor and how do you adjust the document Indentitions
    6·1 answer
  • How to boost audio live ​
    10·1 answer
  • Let x = ["Red", 2.55,"Green", 3,"Black","false"], then solve the following:
    7·1 answer
  • Which option ensures that items in a text box or table cell will be in the absolute center of that element?
    12·1 answer
  • HELP GENIUS Rank :))) DoNT answer If you don't know
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!