student line up sort

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string name;
	int people;
	
	cout << "This will sort the student into alphabetical order.\n\n";

	cout << "Enter the number of people in the class: ";
	cin >> people;
	cout << "\n\n";

	while (people < 1 || people > 25)
	{
		cout << "Please re-enter the number of people, must be greater than 1 or less than or equal to 25.\n\n";
		cout << "Enter the number of people in the class: ";
		cin >> people;
		cout << "\n\n";
	}
	for (int x = 0 ; x <= people ; x++)
	{
		cout << "Enter name: ";
		getline(cin , name);
		cout << endl;
	}
	sort(name.begin() , name.end());
		
	cout << name << endl;
	cout << "\n\n";

	cout << endl << "Press ENTER to exit...";
	cin.clear();
	cin.sync();
	cin.get();

	return 0;
}


i want to sort the students names in alphabetical order using the algorithm sort method.

I know my sort is probably in the wrong place, but i cant get it to sort, it maybe incompplete can someone help me.
1
2
3
4
5
6
for (int x = 0 ; x <= people ; x++)
	{
		cout << "Enter name: ";
		getline(cin , name);
		cout << endl;
	}


This keeps overwriting the value in the name variable. You need to push_back the value into a vector.
where would i put the pushback at the getline( cin , name)
Read this.

http://www.cplusplus.com/reference/stl/vector/


Basically, once you have the name variable, use push_back to put it into the vector.

HTH
heres my new 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
39
40
41
42
43
44
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	string name;
	int people;
	vector<string> names;

	cout << "This will sort the student into alphabetical order.\n\n";

	cout << "Enter the number of people in the class: ";
	cin >> people;
	cout << "\n\n";

	while (people < 1 || people > 25)
	{
		cout << "Please re-enter the number of people, must be greater than 1 or less than or equal to 25.\n\n";
		cout << "Enter the number of people in the class: ";
		cin >> people;
		cout << "\n\n";
	}
	for (int x = 0 ; x <= people ; x++)
	{
		cout << "Enter name (first name only): ";
		cin >> name;
		names.push_back(name);
		cout << endl;
		sort (names.begin() , names.end());
	}
	cout << name << endl;
	cout << "\n\n";

	cout << endl << "Press ENTER to exit...";
	cin.clear();
	cin.sync();
	cin.get();

	return 0;
}


Its only keeping 1 name in the vector and thats the last name i input, any idea how to fix that.
Do the sorting out side the for loop.

You also need a loop to display all the names in the vector.

This bit is only the last name that was entered.

cout << name << endl;
Ok, i will work some more on it, but if i cant figure it out i might need a strart on the loop for the sort algorithm.
i need some help getting the loop and everything started to sort the name,


is it possible you can give me a example loop using sort or example program using sort.
Look at this example:

http://www.cplusplus.com/reference/algorithm/sort/


This is from the reference pages. I don't what is so difficult about the reference pages, that people don't seem to be able to find stuff in there themselves.

You can easily find this by googling C++ vector sort - how difficult was that?

Sorry for whinging, but I am often disappointed at the lack of self help these days.
Its not a problem theideasman, i am not going to complain the truth is your right, i should have done more but i didnt for that i apologize.++
Don't worry - I didn't mean to be so critical. The thing is you are not the only one by a long way.

Happy to help more if needed :=D
ok, :)
Topic archived. No new replies allowed.