Help sorting books in alpha. order - c++

Nov 13, 2011 at 5:40am
Hey guys, can you help me out with this program I'm writing. I'm supposed to type in a specific data of books (which I have as a txt file and I redirect). The program is supposed to sort the books by price and alphabetical order. I got it down with price, but I can't seem to figure out how to do it in alphabetical order. I looked it up, but there's a few different methods to do it, and I'm completely confused with them. Here's my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using namespace std;

struct Books
{
char Title[128];
char Author[256];
float price;

};
Books BookInfo()
{
Books temp;
cout << "Enter the following information for a Book:" << endl;
cout << "Title: ";
cin.getline( temp.Title, 128 );
cout << "Author: ";
cin.getline( temp.Author, 256 );
cout << "Price: ";
cin >> temp.price;
cin.get(); // get rid of the newline character
return temp;	
}




void SortMyBook( Books Byprices[], int num )
{
int i, j;

for( i= 0; i < num - 1; i++ )
{
for( j= i+1; j < num; j++ )

{
Books temp= Byprices[i];
Byprices[i]= Byprices[j];
Byprices[j]= temp;
}
}
}



void OutputGames( Books Libro[], int num )
{
cout << endl;
for( int i= 0; i < num; i++ )
{
cout << "The output of the book list sorted by price :" << endl;
cout << "Title: " << Libro[i].Title << endl;
cout << "Author: " << Libro[i].Author << endl;
cout << "Price: " << Libro[i].price << endl;
cout << endl;
}
}

const int NUMBER= 5;
int main()
{

Books Libro[NUMBER];

for( int i= 0; i < NUMBER; i++ )

	
Libro[i]= BookInfo();

SortMyBook( Libro, NUMBER );

OutputGames( Libro, NUMBER );
system("pause");
}
Last edited on Nov 13, 2011 at 5:40am
Nov 13, 2011 at 6:38am
Heres a hint: compare the Char arrays ;)
Nov 13, 2011 at 8:44am
How can you sort with the price and with the alphabitcal order .. i am little confused .
Nov 13, 2011 at 8:56am
but you can compare like this .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void SortMyBookWithPrice( Books Byprices[], int num )
{
	int i, j;
	for( i= 0; i < num - 1; i++ )
	{
		if( Byprice[i].price < Byprice[i + 1].price ) 
		{
	
			Books temp= Byprices[i];
			Byprices[i]= Byprices[i + 1];
			Byprices[i + 1]= temp;
		}		
	}
}


void SortMyBookWithTitle( Books Byprices[], int num )
{
	int i, j;
	for( i= 0; i < num - 1; i++ )
	{
		if( Byprice[i].Title[i] < Byprice[i + 1].Title[i+1] ) 
		{
	
			Books temp= Byprices[i];
			Byprices[i]= Byprices[i + 1];
			Byprices[i + 1]= temp;
		}		
	}
}
Nov 13, 2011 at 9:00am
frantzdyromain91: char arrays can't be compared (directly).

bluecoder: That doesn't work. In more ways than one.

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
Although I would suggest using std::string instead.
Nov 13, 2011 at 9:53am
sorry for the confusion, it would output a list of the books sorted out by price, and a list sorted out in alphabetical order (by the name of the book). The way it's sorted about by price right now is from least to greatest
Topic archived. No new replies allowed.