So, it sorts the books by price just fine, but I cannot get it to sort the books by title. I am most likely over thinking this, but I've spent a couple hours trying to figure it out. I've looked online and couldn't find the answer. I'm guessing the problem is in the function titled "void SortBookTitle(Book books, int num)". Any feedback would be greatly appreciated.

``````#include <iostream>
using namespace std;
#include <cstring>

// Struct
struct Book
{
char name;
char author;
float price;
};

// Gets Book Information
Book GetBook()
{
Book temp;
cout << "Enter the following information of the book: " << endl;
cout << "Name: ";
cin.getline(temp.name, 100);
cout << "Author: ";
cin.getline(temp.author, 100);
cout << "Price: ";
cin >> temp.price;
cin.get();
return temp;
}

//Sorts the books by Price
void SortBookPrice( Book books[], int num )
{
int i, j;
cout << "Here is a list of your books sorted by Price: " << endl;
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if( books[i].price > books[j].price )
{
Book temp = books[i];
books[i] = books[j];
books[j] = temp;
}
}
}
}

//Sorts the books by title
void SortBookTitle( Book books[], int num )
{
int i, j;
cout << "Here is a list of your book sorted by Title: " << endl;
for(i=0; i<num-1; i++)
for(j=0;j<num-(i+1);j++)
if(strcmp(books[i].name,books[j].name) > 0)
{
Book temp = books[i];
books[i] = books[j];
books[j] = temp;
}
}

//Outputs the information collected
void OutputBooks( Book books[], int num )
{
cout << endl;
for(int i=0;i<num;i++)
{
cout << "Book Information: " << endl;
cout << "Name: " << books[i].name << endl;
cout << "Author: " << books[i].author << endl;
cout << "Price: " << books[i].price << endl;
cout << endl;
}
}

const int NUMBER = 5;
int main()
{
Book books[NUMBER];
for(int i=0;i<NUMBER;i++)
books[i]=GetBook();

SortBookPrice(books, NUMBER);

OutputBooks(books, NUMBER);

SortBookTitle(books, NUMBER);

OutputBooks(books, NUMBER);
}``````

So, it sorts the books by price just fine, but I cannot get it to sort the books by title. I am most likely over thinking this, but I've spent a couple hours trying to figure it out. I've looked online and couldn't find the answer. I'm guessing the problem is in the function titled "void SortBookTitle(Book books, int num)". Any feedback would be greatly appreciated.

## All 2 Replies

int strcmp ( const char * str1, const char * str2 );
Compare two strings

Compares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

Return Value
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

There may be some upper or lower case issues with using strcmp(). strcmp() will compare the ascii value of the non-matching characters and can assume that the character with the lower ascii value will occur first alphabetically (assuming they are of the same case; an upper case 'Z' will have a lower value than a lower case 'a' for example.) Consider using the <cctype> tolower() or toupper() functions to make everything the same.

check out the ascii table.

Also, why are the expressions in your for() loops different between the two Sort() functions? I would think you'd want to iterate over the items in the array the same way, just changing what field you're comparing....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.