Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,914 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,565 people online right now. Registration is fast and FREE... Join Now!




C++ Student ID Sort ... Help with code Please

 
Reply to this topicStart new topic

C++ Student ID Sort ... Help with code Please

Azquadz
18 May, 2008 - 07:17 PM
Post #1

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 1

I am obviously a beginner at C++ coding and yes this is homework. I have written this code and am now confused on what I did wrong. I continue to get syntax errors. I am trying to have a user put in 5 names with student ID and grade, and then I am trying to sort by ID. Thanks for your help.

CODE


#include <iostream>
#include <iomanip>



using std::cout;
using std::cin;
using std::endl;
using std::setw;

void selection_sort(int a[], int length);
int minimum_from(int a[], int position, int length);
void swap(int& first, int& second);



int main()
{      
    struct school
    {
        int student_id;
        char name[30];
        char grade[2];
    };

    struct school table[5] = {0};

    cout << "Enter information for 5 students below" << endl;
    for (int i=0; i<5; i++)
    {
        cout << "Student ID " << i+1 << ": ";
        cin >> table.student_id;
        cin.get();
        cout << "Name " << i+1 << ": ";
        cin.getline((table.name), 30);
        cout << "Grade " << i+1 << ": ";
        cin.getline((table.grade), 30);
        cout << endl;
    };

    cout << setw(20) <<
        "ID" << setw(20) <<
        "Name" << setw(20) <<
        "Grade" << endl;
    
    for (int i=0; i<5; i++)
        cout << setw(20) <<
        table.student_id << setw(20) <<
        table.name << setw(20) <<
        table.grade << endl;

    selection_sort(table, 5);

    return 0;
}



void selection_sort(int school a[], int arrayLength)
{
  for (int count = 0; count < arrayLength - 1; count++)
   swap(a[count],a[minimum_from(a,count,arrayLength)]);
}

int minimum_from(int school a[], int position, int arrayLength)
{
  int min_index = position;

  for (int count = position + 1; count < arrayLength; count ++)
   if (a[count] < a[min_index])
    min_index = count;
   return min_index;
}

void swap(int& first, int& second)
{
  int temp = first;
  first = second;
  second = temp;
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: C++ Student ID Sort ... Help With Code Please
20 May, 2008 - 11:21 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well the biggest problem you are having is that you are trying to use a school struct and passing it to arrays that are expecting integers. My guess is that you are adapting this code from an example you have seen which uses integers. Just remember you must define them as accepting and handling structures.

Another problem you were having is that you defined your struct inside of main. Define them outside of main. That way everyone knows what a school struct is and looks like.

Yet another problem comes from the actual comparing of the students. Since the compiler has no way of knowing how to compare to school structs, you have to tell it to compare the structures members. In this case the student's id. Now if you had defined operators for your struct, then the compiler could determine how to directly compare their worth against one another.

In this case we just tell the compiler to compare each students id and sort based on that.

The last problem comes from how you are loading and accessing your students in the array. You must specify a subscript when using the array to get at each student. You can't simply use "table" because table is a whole array. You have to tell it which student in the array you want. "table[1]" gets the second student of the array and that itself is a struct of type "school".

Here is your code with comments to help you see what is going on...

cpp

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setw;

// These prototypes need to define the passing of school structs, not integers
void selection_sort(struct school a[], int length);
int minimum_from(struct school a[], int position, int length);
void swap(school& first, school& second);

// Define school structure outside of main
struct school
{
int student_id;
char name[30];
char grade[2];
};

int main()
{
struct school table[5] = {0};

cout << "Enter information for 5 students below" << endl;

// Collect student info, but remember it is in an array, so you have to access each through the subscript.
// Notice the use of [i] to get at each student struct in the array
for (int i=0; i<5; i++)
{
cout << "Student ID " << i+1 << ": ";
cin >> table[i].student_id;
cin.get();
cout << "Name " << i+1 << ": ";
cin.getline((table[i].name), 30);
cout << "Grade " << i+1 << ": ";
cin.getline((table[i].grade), 30);
cout << endl;
}

cout << setw(20) <<
"ID" << setw(20) <<
"Name" << setw(20) <<
"Grade" << endl;

// Again we use the subscript to get at each student struct
for (int i=0; i<5; i++)
cout << setw(20) <<
table[i].student_id << setw(20) <<
table[i].name << setw(20) <<
table[i].grade << endl;

cout << endl;

// Pass the array of "school" structs
selection_sort(table, 5);

// Show that they have now been sorted
for (int i=0; i<5; i++)
cout << setw(20) <<
table[i].student_id << setw(20) <<
table[i].name << setw(20) <<
table[i].grade << endl;

return 0;
}


// Takes an array of school structs, not integers
void selection_sort(struct school a[], int arrayLength)
{
for (int count = 0; count < arrayLength - 1; count++)
swap(a[count],a[minimum_from(a,count,arrayLength)]);
}

// Again passes school structs, not integer array
int minimum_from(struct school a[], int position, int arrayLength)
{
int min_index = position;

for (int count = position + 1; count < arrayLength; count++)

// Here we compare the student's id against one another. We don't compare the structs directly
// because we haven't defined operators for the struct. How is the compiler suppose to know how to compare?
if (a[count].student_id < a[min_index].student_id)
min_index = count;
return min_index;
}

// Again we are using school structs, not integers. Temp must also be defined to hold a school struct.
void swap(struct school& first, struct school& second)
{
school temp = first;
first = second;
second = temp;
}


As you read through be sure to take notice of my changes (oh and btw for loops don't use a semicolon at the end). Once you read through the comments you should have a better idea of what is going on.

Enjoy!

"At DIC we be student comparing code ninjas.... DIC ninja > student = true every time" decap.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:53AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month