hey guys !!!
i have to sort the students according to their first and last names
here is the code i have written so far ,, it inputs the first names and last names and then it stops working . please help me with this ,, thank u

#include <iostream>
#include <string>
using namespace std;
struct Student
{


    string FirstName ;
    string LastName ;

};
void inputFromUser (Student *S );
void SortArray (Student *S);
int main ()
{
    Student s ;

    inputFromUser(&s);
    SortArray(&s);
    system("pause");
    return 0 ;
}
void inputFromUser (Student *S)
{

    for ( int i = 0 ; i<10 ; i++)
    {
        Student *S = new Student [10] ;
        cout << " enter the first name " << endl;
        cin >> S[i].FirstName ;
        cout << " enter last name " << endl;
        cin >> S[i].LastName ;

    }

}
void SortArray (Student *S)
{
    string temp[10] ;
    string temp1[10] ;
    for (int i = 0 ; i<10 ; i++ )
    {
        if (S[i].FirstName >S[i+1].FirstName && S[i].LastName > S[i+1].LastName)
        {
            temp[i] = S[i].FirstName ;
            temp1[i] = S[i].LastName ;

            cout << temp[i] << " " << temp1[i] << endl;
        }
    }
}

Recommended Answers

All 7 Replies

There are a number of problems with your code. Most notable are the sort doesn't actually sort, you're not actually using an array, and inputFromUser() is storing data in a local variable that gets leaked away on every iteration of the loop. Worse, you're in SortArray() assuming that this not array has 10 items and it results in stomping all over memory you don't own.

Compare and contrast:

#include <iostream>
#include <string>

using namespace std;

struct student {
    string first_name, last_name;
};

int load_students(student a[], int n);
void show_students(student a[], int n);
void sort_students(student a[], int n);

int main()
{
    student a[10];
    int n = load_students(a, 10);

    sort_students(a, n);
    show_students(a, n);
}

int load_students(student a[], int n)
{
    int i;

    for (i = 0; i < n; i++) {
        cout << "Enter student #" << i + 1 << "'s first and last name (EOF to quit): ";

        if (!(cin >> a[i].first_name) || !(cin >> a[i].last_name)) {
            break;
        }
    }

    return i;
}

void show_students(student a[], int n)
{
    for (int i = 0; i < n; i++) {
        cout << i + 1 << ": " << a[i].first_name << ' ' << a[i].last_name << '\n';
    }
}

void sort_students(student a[], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = n - 1; j > 0; j--) {
            if (a[j].last_name < a[j - 1].last_name) {
                student temp = a[j];
                a[j] = a[j - 1];
                a[j - 1] = temp;
            }
        }
    }
}

thanx bro but i have to create a dynamic array not static array

Then replace student a[10]; with a dynamically created array of ten students.

thanx bro but i have to create a dynamic array not static array

How the array is defined was the least of your problems. Please learn from my example, and feel free to modify it to suit your needs.

how do i keep track of the names entered ?

how do i keep track of the names entered ?

They're in an array...

thanx bro but i have to create a dynamic array not static array

A vector might be what you're looking for

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.