#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
usingnamespace 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.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
usingnamespace 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.
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.