954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Book Sorting Program - Will not sort the books by title. Help?

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[100];
	char author[100];
	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.

Adnan671
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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 tolower() or toupper() functions to make everything the same.

check out the ascii table.

Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
 

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....

raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: