I think I got this logic wrong

Hi I'm trying to write my own database program for strings that sorts them alphabetically and writes them to a file. I'm fine with the text file stuff but the algorithm in the bool sort seems to have stopped working. It was sorting the strings alphabetically before but now it seems to have stopped doing that. Is my logic right in my bool sort function? before I had it so that if 2 strings were the same in objects it would sort the next line alphabetically. Have I got the logic right to sort alphabetically string by string on line 48. If not can someone show me the logic to sort each card alphabetically so I can use the bool function to supply it to sort?
Thanks.
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<fstream>
#include<iomanip>
#include<stdlib.h>

using namespace std;

struct addressCard
{
string name;
string street;
string town;
string county;
string postCode;
string country;
int telephoneNumber;
};


// write to an output stream, object into a stream
ostream& operator<< ( ostream& stm, const addressCard& a )
    // write one string per line, end with a blank line
{
    return stm << a.name << '\n' << a.street << '\n' << a.town << '\n' 
<< a.county << '\n' << a.postCode << '\n' << a.country << "\n\n" ;
}

// read from an input stream
istream& operator>> ( istream& stm, addressCard& a )
{
    // read one line per string, ignore blank lines at the beginning

    // read the first line, after skipping over blank lines
    while( getline( stm, a.name ) && a.name.empty() ) ;

    // read the remaining lines
    if( getline( stm, a.street ) && getline( stm, a.town ) 
&& getline( stm, a.county ) && getline( stm, a.postCode ) 
&& getline( stm, a.country ) ) ; // do nothing more
    else a = {} ; // clear all fields in a

    return stm ;
}

bool genericSort(const addressCard &obj1, const addressCard &obj2)
{
    if (obj1.name == obj2.name)
    {
        return obj1.street < obj2.street;
    }
    else if (obj1.name == obj2.name && obj1.street == obj2.street)
    {
        return obj1.postCode < obj2.postCode;   
    }
    else if (obj1.name == obj2.name && obj1.street == obj2.street
    && obj1.town == obj2.town)
    {
        return obj1.town < obj2.town;   
    }
    else if (obj1.name == obj2.name && obj1.street == obj2.street
    && obj1.town == obj2.town && obj1.county == obj2.county)
    {
        return obj1.postCode < obj2.postCode;   
    }
    else if (obj1.name == obj2.name && obj1.street == obj2.street
    && obj1.town == obj2.town && obj1.county == obj2.county
    && obj1.postCode == obj2.postCode)
    {
        return obj1.country < obj2.country;   
    }

}

const unsigned numberOfPeople = 3;

int main()
{
vector<addressCard> people(numberOfPeople);

for (vector<addressCard>::size_type i = 0; i!=numberOfPeople; ++i)
{
cout <<"Person# " <<i+1<< " name: " << endl;
getline(cin,people[i].name);

cout <<"Person# " <<i+1<< " street: " << endl;
getline(cin,people[i].street);

cout <<"Person# " <<i+1<< " town: " << endl;
getline(cin,people[i].town);

cout <<"Person# " <<i+1<< " county: " << endl;
getline(cin,people[i].county);

cout <<"Person# " <<i+1<< " postCode: " << endl;
getline(cin,people[i].postCode);

cout <<"Person# " <<i+1<< " country: " << endl;
getline(cin,people[i].country);

}

cout <<"\n\n";


cout <<"Sorting....\n";

sort(people.begin(), people.end(), genericSort);

cout <<"Printing results....\n";

for (addressCard &n : people)
{
cout << n.name << endl;
cout << n.street << endl;
cout << n.town << endl;
cout << n.county << endl;
cout << n.postCode << endl;
cout << n.country << endl;

cout <<"\n\n";
}

cout <<"Printing raw vector....\n";

for (vector<addressCard>::size_type i = 0; i!=numberOfPeople; ++i)
{
cout <<people[i].name << endl;

cout <<people[i].street << endl;

cout << people[i].town << endl;

cout <<people[i].county << endl;

cout << people[i].postCode << endl;

cout <<people[i].country << endl;

cout <<"\n\n";

}

cout <<"Writing to file....\n";

const string file_name = "data.txt" ;

        ofstream ofile(file_name) ;
        for( const addressCard& a : people ) ofile << a ; //vector 
//favourites of class type addressCard, object a iterates vector
// and writes it to text file
ofile.close();

cout<<"Writing text file to screen...\n\n";

    string x;
    ifstream inFile;

    inFile.open("data.txt");
    if (!inFile) {
        cout << "Unable to open file";
        return(1); // terminate with error
    }

    while (getline(inFile,x)) {
cout << x << endl ;
}

    inFile.close();

cout <<"Loading text file into a new vector....\n";

        vector<addressCard> addressCardList ; //new vector addressCardList
        ifstream ifile(file_name) ; //ifstream reads text file
        addressCard a ; //new object
        while( ifile >> a ) addressCardList.push_back(a) ; //object a reads from text,
// file, fills new vector film list
        cout << addressCardList.size() << " items were read\n\n" ;

        for( const auto& a : addressCardList ) cout << a ;

return 0;

}
you have some confusion with the `else'
1
2
3
4
if(foo){
   //...
}
else //here we know that `foo' is false 
yet you are doing
1
2
3
4
5
if (obj1.name == obj2.name)
    {
        return obj1.street < obj2.street;
    }
    else if (obj1.name == obj2.name /*false*/ and ... ) 
so you have false and ... which is false, and your code does not enter any of the branches.

then you have a problem with order
1
2
3
4
    if (obj1.name == obj2.name)
    {
        return obj1.street < obj2.street; //¿what if obj1.street == obj2.street? you should have asked for postCode
    }
what you may do
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
bool genericSort(const addressCard &obj1, const addressCard &obj2) {
	if (obj1.name == obj2.name)
		if (obj1.street == obj2.street)
			if (obj1.town == obj2.town)
				if (obj1.county == obj2.county)
					if (obj1.postCode == obj2.postCode)
						return obj1.country < obj2.country;
					else
						return obj1.postCode < obj2.postCode; //same as the correspondent if condition, replace == with <
				else
					return obj1.county < obj2.county;
			else
				return obj1.town < obj2.town;
		else
			return obj1.street < obj2.street;
	else
		return obj1.name < obj2.name;
}

// or if you don't want so much nesting
bool genericSort(const addressCard &obj1, const addressCard &obj2) {
	if (obj1.name not_eq obj2.name)
		return obj1.name < obj2.name;
	if (obj1.street not_eq obj2.street)
		return obj1.street < obj2.street;
	if (obj1.town not_eq obj2.town)
		return obj1.town < obj2.town;
	if (obj1.county not_eq obj2.county)
		return obj1.county < obj2.county;
	if (obj1.postCode not_eq obj2.postCode)
		return obj1.postCode < obj2.postCode;
	return obj1.country < obj2.country;
}

1
2
if ( obj1.name == obj2.name )
else if ( obj1.name == obj2.name && ...

IF obj1.name == obj2.name IS NOT TRUE
THEN you test it again knowing that you will fail?

What does your genericSort return, if every condition yields false?
 In function 'bool genericSort(const addressCard&, const addressCard&)':
75:1: warning: control reaches end of non-void function [-Wreturn-type]



1
2
3
4
5
6
7
8
9
if ( obj1.name < obj2.name ) return true;
else if ( obj1.name > obj2.name ) return false;
// assert: obj1.name == obj2.name

if ( obj1.postCode < obj2.postCode ) return true;
else if ( obj1.postCode > obj2.postCode ) return false;
// assert: obj1.postCode == obj2.postCode

return obj1.country < obj2.country;



Note: You could have:
1
2
3
4
bool operator< ( const addressCard &obj1, const addressCard &obj2 )
{
  return genericSort( obj1, obj2 );
}

... if that is the default way to order them.
Thank you so much for your help. I am awful at logic.
Topic archived. No new replies allowed.