alphabetical order
Oct 1, 2012 at 1:38pm UTC
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
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string.h>
#include <string>
using namespace std;
// Prototypes
void students();
int exit();
// Students Function
void students()
{
int number;
char name[25];
cout << "\n\nEnter the number of students: " ;
cin >> number;
for ( int i = 0 ; i < number ; i++)
{
cout << "Enter first and last name: " ;
cin.getline(name, 25);
}
cout << endl;
_getch();
}
// Exit Function
int exit()
{
cout << "\n\nThis program is exiting." ;
return (0);
}
// Main Function
int main()
{
int choice;
cout << "This will display student names in alphabetical order by first name.\n\n" ;
do
{
cout << "--------------------" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "| 1. Students |" << endl;
cout << "| 2. Exit |" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "--------------------" << endl;
cout << "\n\nEnter Choice: " ;
cin >> choice;
while ( choice < 1 || choice > 2)
{
cout << "\n\nPlease re-enter choice, it must be 1 or 2.\n\n" << endl;
cout << "--------------------" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "| 1. Students |" << endl;
cout << "| 2. Exit |" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "--------------------" << endl;
cout << "\n\nEnter Choice: " ;
cin >> choice;
}
switch ( choice )
{
case 1: students(); break ;
case 2: exit(); break ;
}
} while ( choice !=2 );
_getch();
return 0;
}
Im having trouble in the students function i need the name entered put in alphabetical order but anyway i try i cant seem to get it.
Anyone got any suggestions on how i could do it.
Oct 1, 2012 at 2:56pm UTC
Because std::string has comparison operators (
http://www.cplusplus.com/reference/string/operators/ ), you can store all these student names as strings in a std::vector and use the sort algorithm (
http://www.cplusplus.com/reference/algorithm/sort/ ) to sort the list of students in alphabetical order.
This should work because of the layout of the ASCII-Code, but you might have to check for some special cases like comparison of a space character
' '
with letters or comparison of capital with small letters, depending on which order you want to use (a < A < b < B < ... or a < b < c < ... < A < B < ...)
Last edited on Oct 1, 2012 at 4:34pm UTC
Oct 1, 2012 at 3:57pm UTC
Ok and those links you gave for some reason are coming up as 404 page not found.
Oct 1, 2012 at 4:34pm UTC
That's because the closing bracket ) was added to the link, i edited my post above so that the links work now
Topic archived. No new replies allowed.