Code Doesn't Work Properly

My code doesn't show results after pressing 1 or 2. It just become blank. Why is 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
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


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

size_t readNames(string filename, string surnames[], string firstnames[]);
void sortSurnameFirst (string [], string[], size_t);
void displayNames (string [], string[], size_t);
void sortFirstNames (string [], string [], size_t);

int main()
{
int const SIZE = 1000;
size_t count = 0;
int choice;

string names[SIZE];
string surnames[SIZE];

ifstream inputFile;
    string filename;
    int number;

    cout << "Enter the file name: ";
    cin >> filename;

    inputFile.open(filename.c_str());

if(inputFile)
{

while(count <SIZE && inputFile>>surnames[count] >> names[count])
{
count ++;
}

inputFile.close();

cout << " 1. Sort names by surnames then first names "<<endl;
cout << " 2. Sort names by first names then by surnames "<<endl;
cout<< "Enter your sorting choice: " ;
cin >> choice;

if (choice == 1)
{
cout<< "The names are sorted by surnames then first names"<<endl;
sortSurnameFirst (surnames, names,count);
displayNames(surnames, names,count);
}

if (choice == 2)
{
cout<< "The names are sorted by first names then surnames"<<endl;
sortSurnameFirst (surnames, names,count);
displayNames(surnames, names,count);
}

}

else
{
cout << "The following file doesnt exist" << endl;
}



return 0;
}






void sortSurnameFirst (string surnames[], string names[], size_t size)
{
bool swap;
string sort ;
string temp;
do
{
swap = false;
for (size_t i =0; i < size ; i++)
{
if (surnames[i] > surnames[i++])
{
sort = surnames[i];
surnames[i] = surnames[i++];
surnames[i++] = sort ;
swap = true;


}


if ( surnames[i] == surnames[i++] )
{
if (names[i] > names[i++] )
{
temp= names[i];
names[i] = names[i++];
names[i++] = temp ;
sort = surnames[i];
surnames[i] = surnames[i++];
surnames[i++] = sort ;

swap = true;
}
}



}

}
while (swap);
}


void displayNames( string surnames[], string names[], size_t size )
{

for ( size_t i =0; i < (size - 1) ; i++)
{
cout << surnames[i]<<" " << names[i] << endl;
}
}


void sortFirstNames (string surnames[], string names[], size_t size)
{
bool swap;
string sort1, temp1;
do
{
swap = false;

for ( size_t i =0; i < size ; i++)
{
if ( names[i] >names[i++] )
{
sort1 = names[i];
names[i] = names[i++];
names[i++] = sort1 ;
swap = true;

}
if ( names[i] == names[i++] )
{
if (surnames[i] > surnames[i++] )
{
temp1 = surnames[i];
surnames[i] = surnames[i++];
surnames[i++] = temp1 ;
sort1 = names[i];
names[i] = names[i++];
names[i++] = sort1 ;
swap = true;
}
}



}

}while (swap);
}
Hello MasterKay,

With a quick look at your code the first thing I see is:

1
2
3
4
5
6
7
8
for (size_t i =0; i < size ; i++)
{
if (surnames[i] > surnames[i++])
{
sort = surnames[i];
surnames[i] = surnames[i++];
surnames[i++] = sort ;
swap = true;


By the time you finish with line 7 "i" will have the value of 4 because the "i++".

What you should have is:
1
2
3
4
5
6
7
8
for (size_t i =0; i < size ; i++)
{
if (surnames[i] > surnames[i + 1])
{
sort = surnames[i];
surnames[i] = surnames[i + 1];
surnames[i + 1] = sort ;
swap = true;


Second every sort routine I have seen is done with a nested for loop:

1
2
3
4
5
6
7
for (size_t i = 0; i < size; i++)
{
    for (size_t j = 1; j < size; j++)
    {
        // sort code here.
    }
}


There is a start until I can test the program.

Hope that helps,

Andy
Hello MasterKay,

Some things I found while testing the program:

1. In the "sortSurnameFirst" function Keep in mind that any swap routine needs to swap two arrays not just one or not one at a time.

2. You can only sort by last name or first name. Not both at the same time. Otherwise this will jumble you names.

3. This code, starting at line 63, will never be seen because after it prints to the screen the program ends:

1
2
3
4
else
{
cout << "The following file does not exist" << endl;
}

after the "cout" you need way to pause before the program ends.

4. This prototype has no function definition. size_t readNames(string filename, string surnames[], string firstnames[]);. You have done the read in main.

Hope that helps,

Andy
The program still giving me problems... The names are not sorted properly.

Andy, please fix it like you tell me and try to run it. Create a random txt file titled "names" first and do it on your PC. If if works please show me the codes.

Thank you so far!
The program should be 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

Name Sorting
Enter the file name: names.txt
1. Sort names by surnames then first names
2. Sort names by first names then surnames
Enter your sorting choice: 1
-----------------------------------------------------
Surname First Name
-----------------------------------------------------
Adams Grace
Adams Hein
Adams Samantha
Babajide Pretty
chadwick billy
deWet Abel
Olivier Vreda
Venter Charlie
Name Sorting
-----------------------------------------------------
Enter the file name: names.txt
1. Sort names by surnames then first names
2. Sort names by first names then surnames
Enter your sorting choice: 2
-----------------------------------------------------
Surname First Name
-----------------------------------------------------
deWet Abel
chadwick billy
Venter Charlie
Adams Grace
Adams Hein
4
Babajide Pretty
Adams Samantha
Olivier Vreda

Last edited on
Topic archived. No new replies allowed.