How do I sort my strings to print them out?

How do I sort my strings to print them out?
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<iomanip>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;

int main()
 {
    string leslie, robert, alice;
	string john,
		helen,
		raphael,
		sally,
		jason,
		michael,
		dutch,
		amy,
		butch,
		francisco,
		benjamin,
		emma,
		charles,
		leo,
		jamelle,
		yvette,
		nathan,
		lizzy,
		arturo,
		jeane,
		betty1,
		maria;
string haruto,
		benito,
		susan,
		muhammed,
		garis,
		harvey,
		kanna,
		jorge,
		rosa,
		bob,
		malek,
		nancy,
		stephanie,
		lucia,
		francis,
		jamal,
		oprah,
		peter,
		elizabeth,
		marquis,
		cara,
		william;
string randy,
		billy,
		hannah,
		jan,
		dimitris,
		betty2,
		li,
		brianna,
		seojun,
		pedro,
		amara,
		lucas,
		gloria,
		dylan,
		deshawn,
		arman,
		juana,
		patricia,
		ernst,
		leticia,
		david,
		riku,
		martin;

	std::ifstream file("Lineup1.txt"); // input file
	
{
    if(file) 
	{
	  ostringstream ss; // takes the string and saves it
      ss << file.rdbuf(); // accesses stream object
      leslie = ss.str(); // changes the content of file into the string grocery
	}
	else
	{
    	cout << "Error opening file";
	}
	unsigned i=0;
	for(;i<5;){
    	john += leslie.at(i);
    	i++;
	}
	for(i=5;i<10;){
    	helen += leslie.at(i);
    	i++;
	}
	for(i=11;i<18;){
    	raphael += leslie.at(i);
    	i++;
	}
	for(i=19;i<24;){
    	sally += leslie.at(i);
    	i++;
	}
	for(i=25;i<30;){
    	jason += leslie.at(i);
    	i++;
	}
	for(i=31;i<38;){
    	michael += leslie.at(i);
    	i++;
	}
	for(i=39;i<44;){
    	dutch += leslie.at(i);
    	i++;
	}
	for(i=45;i<48;){
    	amy += leslie.at(i);
    	i++;
	}
	for(i=49;i<54;){
    	butch += leslie.at(i);
    	i++;
	}
	for(i=55;i<64;){
    	francisco += leslie.at(i);
    	i++;
	}
	for(i=65;i<73;){
    	benjamin += leslie.at(i);
    	i++;
	}
	for(i=74;i<78;){
    	emma += leslie.at(i);
    	i++;
	}
    for(i=79;i<86;){
    	charles += leslie.at(i);
    	i++;
	}
	for(i=87;i<90;){
    	leo += leslie.at(i);
    	i++;
	}
	for(i=91;i<98;){
    	jamelle += leslie.at(i);
    	i++;
	}
	for(i=99;i<105;){
    	yvette += leslie.at(i);
    	i++;
	}
	for(i=106;i<112;){
    	nathan += leslie.at(i);
    	i++;
	}
	for(i=113;i<118;){
    	lizzy += leslie.at(i);
    	i++;
	}
	for(i=119;i<125;){
    	arturo += leslie.at(i);
    	i++;
	}
	for(i=126;i<131;){
    	jeane += leslie.at(i);
    	i++;
	}
	for(i=132;i<137;){
    	betty1 += leslie.at(i);
    	i++;
	}
	for(i=138;i<143;){
    	maria += leslie.at(i);
    	i++;
	}
}
	
	ifstream inFile("Lineup2.txt"); // input file
{	
    if(inFile) 
	{
	  ostringstream ss; // takes the string and saves it
      ss << inFile.rdbuf(); // accesses stream object
      robert = ss.str(); // changes the content of file into the string grocery
	}
	else
	{
    	cout << "Error opening file";
	}
	unsigned i=0;
	for(;i<6;){
    	haruto += robert.at(i);
    	i++;
	}
	for(i=7;i<13;){
    	benito += robert.at(i);
    	i++;
	}
	for(i=14;i<19;){
    	susan += robert.at(i);
    	i++;
	}
	for(i=20;i<28;){
    	muhammed += robert.at(i);
    	i++;
	}
	for(i=29;i<34;){
    	garis += robert.at(i);
    	i++;
	}
	for(i=35;i<41;){
    	harvey += robert.at(i);
    	i++;
	}
	for(i=42;i<47;){
    	kanna += robert.at(i);
    	i++;
	}
	for(i=48;i<53;){
    	jorge += robert.at(i);
    	i++;
	}
	for(i=54;i<58;){
    	rosa += robert.at(i);
    	i++;
	}
	for(i=59;i<62;){
    	bob += robert.at(i);
    	i++;
	}
	for(i=63;i<68;){
    	malek += robert.at(i);
    	i++;
	}
	for(i=69;i<74;){
    	nancy += robert.at(i);
    	i++;
	}
	for(i=75;i<84;){
    	stephanie += robert.at(i);
    	i++;
	}
	for(i=85;i<90;){
    	lucia += robert.at(i);
    	i++;
	}
	for(i=91;i<98;){
    	francis += robert.at(i);
    	i++;
	}
	for(i=99;i<104;){
    	jamal += robert.at(i);
    	i++;
	}
	for(i=105;i<110;){
    	oprah += robert.at(i);
    	i++;
	}
	for(i=111;i<116;){
    	peter += robert.at(i);
    	i++;
	}
	for(i=117;i<126;){
    	elizabeth += robert.at(i);
    	i++;
	}
	for(i=127;i<134;){
    	marquis += robert.at(i);
    	i++;
	}
	for(i=135;i<139;){
    	cara += robert.at(i);
    	i++;
	}
	for(i=140;i<147;){
    	william += robert.at(i);
    	i++;
	}
}
	inFile.close();
	fstream yes("Lineup3.txt"); // input file
{	
    if(yes) 
	{
	  ostringstream ss; // takes the string and saves it
      ss << yes.rdbuf(); // accesses stream object
      alice = ss.str(); // changes the content of file into the string grocery
	}
	else
	{
    	cout << "Error opening file";
	}
	unsigned i=0;
	for(;i<5;){
    	randy += alice.at(i);
    	i++;
	}
	for(i=6;i<11;){
    	billy += alice.at(i);
    	i++;
	}
	for(i=12;i<18;){
    	hannah += alice.at(i);
    	i++;
	}
	for(i=19;i<22;){
    	jan += alice.at(i);
    	i++;
	}
	for(i=23;i<31;){
    	dimitris += alice.at(i);
    	i++;
	}
	for(i=32;i<37;){
    	betty2 += alice.at(i);
    	i++;
	}
	for(i=38;i<40;){
    	li += alice.at(i);
    	i++;
	}
	for(i=41;i<48;){
    	brianna += alice.at(i);
    	i++;
	}
	for(i=49;i<55;){
    	seojun += alice.at(i);
    	i++;
	}
	for(i=56;i<61;){
    	pedro += alice.at(i);
    	i++;
	}
	for(i=62;i<67;){
    	amara += alice.at(i);
    	i++;
	}
	for(i=68;i<73;){
    	lucas += alice.at(i);
    	i++;
	}
	for(i=74;i<80;){
    	gloria += alice.at(i);
    	i++;
	}
	for(i=81;i<86;){
    	dylan += alice.at(i);
    	i++;
	}
	for(i=87;i<94;){
    	deshawn += alice.at(i);
    	i++;
	}
	for(i=95;i<100;){
    	arman += alice.at(i);
    	i++;
	}
	for(i=101;i<106;){
    	juana += alice.at(i);
    	i++;
	}
	for(i=107;i<115;){
    	patricia += alice.at(i);
    	i++;
	}
	for(i=116;i<121;){
    	ernst += alice.at(i);
    	i++;
	}
	for(i=122;i<129;){
    	leticia += alice.at(i);
    	i++;
	}
	for(i=130;i<135;){
    	david += alice.at(i);
    	i++;
	}
	for(i=136;i<140;){
    	riku += alice.at(i);
    	i++;
	}
	for(i=141;i<147;){
    	martin += alice.at(i);
    	i++;
	}
}
	
	
	cout<<"  Lineup    Teacher   First Student   Last Student   "<<endl;
	cout<<"  -------  ---------  -------------  --------------  "<<endl;
	cout<<"        1   Leslie    "<<leslie<<endl;
	cout<<"        2   Robert    "<<endl;
	cout<<"        3   Alice     "<<endl;
	
    return 0;
 }
Something in this program has gone very very wrong. This feels like you've in some way really missed the point of programming, or missed the point of C++, or something.

Anyway, typically you would sort strings by putting them all in a vector of strings, and then use the sort function.

1
2
vector<string> allTheStrings; // All the strings go in here, in this vector, which is just a container that holds lots of strings
sort(allTheStrings.begin(), allTheStrings.end()); // Sort them in the container. Sort them from beginning to end. 


So that's how we would typically sort strings. Your program, though, just looks very very wrong. So many strings, each one with a person's name. What is going on here? What are you actually trying to do?
Well im a beginner and my teacher assigned us to take names from a file, and order the alphabetically to print the first one and the last student in a line. "Three Elementary School teachers have asked all of their students in their classes to line up in single files (one for each class) according to their first names. For example, in one class, Amy will be at the front of the line and Yvette at the end of the line. The first names of the students in each class are given in the files Lineup1.txt, Lineup2.txt, and Lineup3.txt at the bottom of this assignment. These files correspond to the classes for teachers Leslie, Robert, and Alice, respectively."
Well then, you want code that does something like this:


1
2
3
4
5
6
7
8
9
10
11
12
  vector<string> names_from_file;
  std::ifstream file("Lineup1.txt");

  string name_from_file;
  while (file >> name_from_file) // READ A NAME FROM A FILE
    {
      names_from_file.push_back(name_from_file); // Put the name you read into the vector
    }
  sort(names_from_file.begin(), names_from_file.end()); //Sort the names

  int number_of_names = names_from_file.size();
  cout << "First name: " << names_from_file[0] << "   Last name: " << names_from_file[number_of_names-1];


Watch out for empty input files, and watch out for different case in names (in a std sort, capital letters all come before small letters; 'a' comes after 'Z'.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;

vector<string> getNames( istream &in )
{
   set<string> S( istream_iterator<string>{ in }, {} );
   return vector<string>( S.begin(), S.end() );
}


int main()
{
// ifstream in( "lineup1.txt" )
   istringstream in( "leslie robert alice john helen raphael harry jason" );
   for ( string s : getNames( in ) ) cout << s << '\n';
}


alice
harry
helen
jason
john
leslie
raphael
robert
Hello nikovch,


How do I sort my strings to print them out?


First you need to read your files and put the names in some type of container.

Now that you have post what the program should do. You need to post the 3 input files, so everyone can see what you are working with. It is better to not make people guess what the input files look like and the names that they contain.

Next open the file streams. You can do this one at a time or all 3 at the same time. If you do this one at a time you can use the same file stream for the 3 different file names. Be sure to check that the file opened and the stream is usable.

Some notes on your code:
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
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<iomanip>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
    string leslie, robert, alice;
    string john, // 1
        helen,
        raphael,
        sally,
        jason,
        michael,
        dutch,
        amy,
        butch,
        francisco,
        benjamin,
        emma,
        charles,
        leo,
        jamelle,
        yvette,
        nathan,
        lizzy,
        arturo,
        jeane,
        betty1,
        maria;  // 22
}  //  End "main" 

Your include look like you included header files, but are not sure if you really need them.

"<sstream>" could be useful, but for what you need to do it is not necessary. You can just read the file for what you need to do.

"<set>". Do you even know what this is for? I do not recall seeing any code that would need this header file.

"<stdio.h>" and "<stdlib.h>". These are C header files and should not be used in a C++ program. What you should use is "<csdtio>" and "<cstdlib>", but still those are not necessary.

In "main" line 15 defines 3 variables of type "std::string". This may work, but it is not what you want.

Line 16 defines 22 variables not individual strings. Not what you want because as far as I know you can not sort variable names.

What you have to tell us is what you can use. Is it an array of strings? Can you use a "std::vector"? Or can you use something else?

I would start with cutting down the include files that you do not need then open a file and get it to read 1 name then expand from there.

When dealing with files I came up with this code early until I had a better understanding of how C++ works:
1
2
3
4
5
6
7
8
9
10
const std::string inFileName{ "" };  // <--- Put file name here.

std::ifstream inFile(inFileName);

if (!inFile)
{
	std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
	
	return 1;
}

This code can be shortened and when you figure that out then you have learned something.

The "std::quoted" in line 7 is from the "<iomanip>" header file and unless someone tells you about it you may never learn about it. What it does is put double quotes around what is in the (), which could be a quoted string or a string variable. See https://en.cppreference.com/w/cpp/io/manip/quoted

When used in "main" the "return 1" leaves the program because there is no point in continuing until you fix the problem.

Andy
Topic archived. No new replies allowed.