My sorting algorithm works in reverse(Z-A) not A-Z

How do i make it sort from A-Z? The function is at the very bottom.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>
using namespace std;

struct library{
	string title;
	string author;
	string publisher;
	int isbn;
};
const int MAX_SIZE = 50;

void enter(library book[], int & size);
void display(const library book[], int size);
void save(const library book[], int size);
void read(library book[], int& size);
bool compareByName(library const &a,library const &b);
void organize (library book[], int size);

int main(int argc, char *argv[]){
int size = 0;
library book[MAX_SIZE];
int numOfRecs = 0;
bool run = true;
char option;
do{	
 		cout << "(e)nter item, (d)isplay, (s)ave, (r)ead, (o)rganize, (q)uit" << endl;
		cin >> option;
		switch(option){
		
			case 'e'|'E': enter(book, size);  break;
			case 'd'|'D': display(book, size); break;
			case 's'|'S': save(book, size); break;
			case 'r'|'R': read(book, size); break;
			case 'o'|'O': organize(book, size); break;
			case 'q'|'Q': run = false; break;
			default : cout << "That was not an option." << endl; break;
		}
	}while(run);
}
	
void enter(library book[], int & size){
	
library tmp; 

 	cout << "title: ";
 	 cin.ignore(256, '\n');
    getline(cin, tmp.title); 
    cout << "author: ";
   	getline(cin, tmp.author); 
    cout << "publisher: ";
  	 getline(cin, tmp.publisher); 
    cout << "ISBN: ";
    cin >> tmp.isbn;
    cout << endl;
	book[size++] = tmp;
    

}

void display(const library book[], int size)
{
  if(size < 1) {
    cout << "No books recorded" << endl;
  } else {
    cout << "List of books" << endl << endl;
    cout << fixed << setprecision(2);   
 
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout <<"Title: "<< book[i].title << endl;
      cout <<"Author: "<< book[i].author << endl;
      cout <<"Publisher: " <<book[i].publisher<< endl;
      cout <<"ISBN:  "<< book[i].isbn << endl << endl;


    }
  }
}

void save(const library book[], int size) {
  ofstream outfi("books.dat");
  
  if (!outfi.fail()) { 
    cout << "Saving books ";
    
    for(int i = 0; i < size; i++) {
      outfi << book[i].title <<  '\n' 
            << book[i].author <<  '\n'
             << book[i].publisher <<  '\n'
            << book[i].isbn;
      // Start a new line after all but the last record
      // Simplifies reading the file as EOF is at end of last line
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << "Books saved" << endl;
    outfi.close();
  } 
  else {
    cout << "Error" << endl;
  }
}

void read(library book[], int& size)
{
  ifstream infi("Inventory.txt");
  string str;
  stringstream strstrm;

  if (!infi.fail()) { 
    cout << "Reading disk ";
    
    size = 0;
    while(!infi.eof() && size < MAX_SIZE) {
      getline(infi, str, '\n'); 
      book[size].title = str;
      
      getline(infi, str, '\n');
      strstrm.str(""); strstrm.clear(); 
      strstrm << str; 
      strstrm >> book[size].author;
      
      getline(infi, str, '\n');
      strstrm.str(""); strstrm.clear(); 
      strstrm << str; 
      strstrm >> book[size].publisher;
      
      getline(infi, str); 
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> book[size++].isbn;
    }
    cout << endl << size << " Books retrieved." << endl;
  }
  else { // something went wrong with opening the file
    cout << "ERROR" << endl;
  }

}

bool compareByName(library const &a, library const &b){
 return a.title > b.title;
}

void organize(library book[], int size){

 if (size > 1){
   std::sort(book, book + MAX_SIZE, compareByName); 
   cout << "Program has sorted the records by Author's first name" << endl << endl; 
 } // end of if
 else cout << "Need at least two names to sort!" << endl << endl; 
 

} 






Last edited on
1
2
3
4
5
bool compareByName(library const &a, library const &b){

    // return a.title > b.title; // order as: larger smaller 
    return a.title < b.title; // order as: smaller larger
}
When I do that all the strings that are suppose to be displayed disappear and the ISBN shows up as random numbers.
The read() function is truncating strings, and not handling input failure (eof) correctly.
(The check for failure should be after attempting input)

For the file created by the save() function, read()should be something 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
31
32
33
34
35
36
37
void read( library book[], int& size ) {

    // ifstream infi("Inventory.txt");
    std::ifstream infi("books.dat"); // read back the same file that was written into?
    // string str;
    // stringstream strstrm;

    if ( infi.is_open() ) {
        std::cout << "Reading disk ";

        size = 0;
        while( size < MAX_SIZE ) {

            std::getline( infi, book[size].title ) ; // try to read a complete line into title
            if( infi.fail() ) break ; // input failed; nothing more to be read

            std::getline( infi, book[size].author ) ; // read the next line into author
            std::getline( infi, book[size].publisher ) ; // read the next line into publisher
            infi >> book[size].isbn ; // read the integer in the next line into isbn
            
            if( infi.fail() ) { // something went wrong

                std::cout << "encountered badly formatted input\n" ;
                break ;
            }

            infi.ignore( 10000, '\n' ) ; // throw away the new line at the end
            ++size ; // go back and read the next item
        }

        std::cout << '\n' << size << " Books retrieved." << '\n';
    }

    else { // something went wrong with opening the file
        std::cout << "ERROR opening input file" << '\n';
    }
}
Last edited on
Topic archived. No new replies allowed.