Thread: Sorting Structs and Debug Help!

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    22

    Sorting Structs and Debug Help!

    Hey, I am currently writing a "grade book" program for my first year programming class. I'm pretty sure most of my code is correct but I'm having trouble with my sort function. Basically, I want to sort them in ascending order by ID number.

    Code:
    //Sort the grade book.
    
    int sort( student students[], const int numstudent)
    {
      for (int i=0; i < numstudent; i++){
        int minindex = i;
        for (int j=i+1; j < numstudent; j++){
          if(students[j].id < students[minindex].id)
            minindex = j;
        }
      student temp =  students[minindex];
      students[minindex] = students[i];
      students[i] = temp;
      }
    }
    Also, I'm having trouble with these compiler errors...any suggestions?

    Code:
    bash-2.03$ g++ -Wall prog16.cc
    prog16.cc: In function `int addStudent(student*, int&)':
    prog16.cc:88: warning: control reaches end of non-void function
    prog16.cc: In function `int sort(student*, int)':
    prog16.cc:125: warning: control reaches end of non-void function
    prog16.cc: In function `float addExam(student*, int)':
    prog16.cc:194: warning: control reaches end of non-void function
    Here is my entire code...

    Code:
    #include<iostream>
    using namespace std;
    
    //Struct Declaration.
    
    struct student
    {
      int id;
      string name;
      float midterm;
      float final;
    };
    
    
    //Function Prototypes.
    
    int addStudent(student students[], int &numstudent);
    float addExam(struct student students[], const int numstudent);
    //float updateExam(struct student students[], const int numstudent);
    void display(student students[], const int numstudent);
    int sort(student students[], const int numstudent);
    int menu();
    
    
    
    
    //Main Function.
    
    int main()
    {
      student s[100];
      int numstudent = 0;
      int option;
    
      cout << "Welcome to E-Grade Book" << endl;
    
      do {
      option = menu();
    
      if(option == 1)
        addStudent(s, numstudent);
      if(option == 2)
        addExam(s, numstudent);
      //if(option == 3)
        //updateExam();
      if(option == 4)
        display(s, numstudent);
      //if(option == 5)
        }while(option != 5);
    
        return 0;
    }
    
    //Add a new student.
    
    int addStudent(student students[], int &numstudent)
    {
      int id;
      string name;
    
      cout << endl;
      cout << endl;
    
      do{
        cout << "Enter a new student's id: ";
        cin >> id;
      } while(id < 1);
        cout << "Enter a new student's name: ";
        cin >> name;
        cout << endl;
        cout << endl;
    
          for (int i=0; i < numstudent; i++){
        if (id == students[i].id){
          cout << "Student id already exists." << endl;
              return 0;
        }}
    
       students[numstudent].id = id;
       students[numstudent].name = name;
       students[numstudent].midterm = -1;
       students[numstudent].final = -1;
       numstudent++;
    
    }
    
    //Display menu of options.
    
    int menu()
    {
      int option;
      do{
        cout << "-------------------------------" << endl;
        cout << "| 1.) Add a student            | " << endl;
        cout << "| 2.) Add an exam score        | " << endl;
        cout << "| 3.) Update an exam score     | " << endl;
        cout << "| 4.) Display the E-Grade Book | " << endl;
        cout << "| 5.) Quit                     | " << endl;
        cout << "|------------------------------| " << endl;
        cout << "| Select an option(1-5):  ";
          cin >> option;
        cout << "|------------------------------|" << endl;
       }  while((option < 1) || (option > 5));
        return option;
    }
    
    //Display menu of options.
    
    int menu()
    {
      int option;
      do{
        cout << "-------------------------------" << endl;
        cout << "| 1.) Add a student            | " << endl;
        cout << "| 2.) Add an exam score        | " << endl;
        cout << "| 3.) Update an exam score     | " << endl;
        cout << "| 4.) Display the E-Grade Book | " << endl;
        cout << "| 5.) Quit                     | " << endl;
        cout << "|------------------------------| " << endl;
        cout << "| Select an option(1-5):  ";
          cin >> option;
        cout << "|------------------------------|" << endl;
       }  while((option < 1) || (option > 5));
        return option;
    }
    
    //Sort the grade book.
    
    int sort( student students[], const int numstudent)
    {
      for (int i=0; i < numstudent; i++){
        int minindex = i;
        for (int j=i+1; j < numstudent; j++){
          if(students[j].id < students[minindex].id)
            minindex = j;
        }
      student temp =  students[minindex];
      students[minindex] = students[i];
      students[i] = temp;
      }
    }
    
    //Display the grade book.
    
    void display(student students[], const int numstudent)
    {
      int sort();
      cout << endl;
      cout << "Id" << '\t' << "Name" << '\t' << '\t' << "Midterm" << '\t' << "Final" << endl;
      cout << "-----------------------------------------------" << endl;
      for (int i=0; i < numstudent; i++){
        cout << students[i].id << '\t' << students[i].name << '\t' << '\t' << students[i].midterm << '\t' << students[i].final << endl;
        }
      cout << "-----------------------------------------------" << endl;
      cout << endl;
      cout << endl;
    
    }
    
    //Add an exam.
    
    float addExam(struct student students[], const int numstudent)
    {
      char selection;
      int id;
      float midterm;
      float final;
    
      cout << "------------------------------" << endl;
      cout << "| Choose Exam (m, f) : ";
        cin >> selection;
      cout << "------------------------------" << endl;
      cout << endl;
    
      do{
          cout << "Enter student's id: ";
          cin >> id;
          }while (id < 0);
    
      for (int i = 0; i < numstudent; i++){
        if(students[i].id == id)
          id = i;
        else{
          cout << endl;
          cout << "Sorry, no student with id " << id << "exists." << endl;
          cout << "Plesae try the add student option." << endl;
          cout << endl;
          cout << endl;
          return 0;
          }
    
      if(selection == 'm'){
        do{
          cout << "Enter student's midterm score: ";
          cin >> midterm;
          }while ((midterm < 0) || (midterm > 100));
    
      students[i].midterm = midterm;
      }
    
      if(selection == 'f'){
        do{
          cout << "Enter student's final score: ";
          cin >> final;
          }while ((final < 0) || (midterm > 100));
    
      students[i].final = final;
      }
    }}
    Thanks guys!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm having trouble with these compiler errors...any suggestions?
    Those are warnings. They mean just what they say. You have a function that says it will return something, but the function ends and nothing is returned. Either change the function to return void, or add a return statement to actually return a value.

    >> int sort();
    That's not how you call a function. Get rid of the int there and pass the appropriate function arguments.

    Your actual sort function looks ok at first glance, but the rest of the code appears to have some mismatched braces, a duplicate function definition, and possibly other issues.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Thanks! I'll take a look at that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MS VC++ 6 Debug Help
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 01-19-2005, 03:28 AM
  2. Pointers to structs
    By csmatheng in forum C Programming
    Replies: 2
    Last Post: 09-11-2002, 03:47 AM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. How to tackle a debug assertion failure?
    By juhigarg in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 12:59 PM