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.
Also, I'm having trouble with these compiler errors...any suggestions?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; } }
Here is my entire code...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
Thanks guys!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; } }}![]()



LinkBack URL
About LinkBacks



