I have read many topics regarding sort but i wasn`t able to sort them. I am new in C++. What changes i must do to sort the names alphabetically ?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class classroom
{
public:
    string studentName;

    void classroom::createStudent(string);
};

void classroom::createStudent(string name)
{
    studentName=name;
}

int main()
{
    //number of students
    const int numstudents=10;
    
    //create structure
    classroom student[numstudents];

    //initialize variables
    student[0].createStudent("Alexa Trina");
    student[1].createStudent("George Ali");
    student[2].createStudent("Comina Riviera");
    student[3].createStudent("Dimitri Askin");
    student[4].createStudent("Erato Georgin");
    student[5].createStudent("Georgina Mare");
    student[6].createStudent("Konan Varvi");
    student[7].createStudent("Lampros Anesti");
    student[8].createStudent("Marina Parate");
    student[9].createStudent("Nikon Smith");

    return 0;//indicate that program end succesfully
}//end main

Thanks for you time!

Recommended Answers

All 5 Replies

I figure out how to do it but with a simple problem.
I add to the previous post`s code the following

bool doMore;

    do {
        doMore = false;  // assume this is last pass over array
        for (int i=0; i<10-1; i++) {
            if (student[i].studentName > student[i+1].studentName) {
                // exchange elements
                classroom temp = student[i]; student[i] = student[i+1]; student[i+1] = temp;
                doMore = true;  // after exchange, must look again
            }
        }
    } while (doMore);

        for (int j=0;j<10;j++)
        {
        cout<<student[j].studentName;
        cout<<endl;
        }

but i got OUTPUT

Comina RivierbAlexa Trina
Comina Riviera
Comina Rivierb
Dimitri Askin
Erato Georgin
Georgina Mare
Konan Varvi
Lampros Anesti
Marina Parate
Nikon Smith

The first Comina Rivierb shouldn`t be there. What is the mistake in my code ?

Post your full code so that one can see.
May be it is hidden it your "hidden" code :)

Post your full code so that one can see.
May be it is hidden it your "hidden" code :)

I just found a wrong cout! That was the problem.

Thanks all for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.