Hi I need to write a program that allows me to enter names in a loop and than it sorts all those names in alphabetical order. Only problem is I really dont know how to do that with compare string in a loop. I really need help heres what I have. Im just messing to see if it works and it does but im trying to make it sort all the names I enter.
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
const int SIZE = 40;
char name[SIZE];
char lastName[SIZE];
char lastName1[SIZE];
int numStudents;
cout << "Enter the name of the student who is at the bottom of last names." << endl;
cin.getline(lastName, SIZE);
cout << "Enter the name of the student who is at the top of the last names." << endl;
cin.getline(lastName1, SIZE);
cin.ignore();
cout <<"Enter how many students you want to sort." << endl;
cin >> numStudents;
for (int count = 1; count <=numStudents; count++)
{
cout << "Enter the name of student " << count << " Last name first" << endl;
cin.ignore();
cin.getline(name, SIZE);
if (strcmp(name, lastName) > 0)
cout << name << " " << lastName << endl;
if (strcmp(name, lastName) < 0)
cout << name << " " << lastName << endl;
If you are including cstring...you might as well just use the C++ string, since it is a lot easier to compare with and sort. Just use the string functions to get/find the letters inside the strings.