0

edit. I want to thank the two people who helped me in my code. I fixed my coding as of 7:22 PM PST 9/25/2014. Still having trouble with the sorting though.

So I'm trying to teach myself how to use C++ in order to get ahead of my programming class in school. I've already taken Java so I'm relatively familiar with the structure of the coding but not exactly sure what to be typing. This practice problem is:

1)Ask the user how many people he wants to enter into the list.

2)Read 3 strings, then turn that info into a student class and put the student inside a vector.

3)Sort the list by name. (Extra credit would be to sort the list by Student ID)

4)Print the info of each student in the list.

5)Ask user while answer is 'Y', search the list for a name and print the info correlating to the name

While in Java, this seems pretty easy, I'm having an extremely hard time understanding what is going on in C++.

I currently only have 1, 2, 4 and 5 done. This is what I have so far.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iomanip>

using namespace std;

class Student
{
    string myName, myStudentID, myClassID;
public:
    Student(string name, string studentID, string classID)
    {
        myName = name;
        myStudentID = studentID;
        myClassID = classID;
    }

    string getName() { return myName; }
    string getStudentID() { return myStudentID; }
    void printInfo() { cout << myName << setw(15) << myStudentID << setw(10) << myClassID << endl; }



};

int main()
{
    int num;
    std::vector<Student> studentList;
    cout << "How many students do you wish to add to the student list ? " << endl;
    cin >> num;


    cin.ignore();
    for (int a = 0; a < num; a++)
    {

        string inputName, inputStudentID, inputClassID;
        //get name
        cout << "Please enter the Student name : ";
        getline(cin, inputName);


        //get student ID
        cout << "Please enter the Student ID number : ";
        getline(cin, inputStudentID);


        //get class ID
        cout << "Please enter the Student class ID : ";
        getline(cin, inputClassID);



        Student thisStudent(inputName, inputStudentID, inputClassID);
        studentList.push_back(thisStudent);

        cout << endl;
    }

    //sort(studentList.begin(), studentList.end());
    /*
    I never figured out how to sort the list by name.
    I do not know how to compare the name of studentList[0] and studentList[1]
    to put them in alphabetical order.
    */
    cout << endl;; // FORMATTING

    cout << "The student list has a size of " << studentList.size() << endl;

    cout << endl;; // FORMATTING


    cout << "Student Name" << setw(15) << "Student ID" << setw(10) << "Class ID" << endl;
    for (int a = 0; a < studentList.size(); a++)
    {
        studentList[a].printInfo();
    }

    cout << endl;; // FORMATTING


    string searchedName;
    char answer;
    do
    {
        cout << endl;; // FORMATTING
        cout << "Please type the name of the person you want to search for: ";
        getline(cin, searchedName);
        for (int a = 0; a < studentList.size(); a++)
        {
            if (searchedName.compare(studentList[a].getName()) == 0)
            {
                studentList[a].printInfo();
                break;
            }
            else
                if (a == (studentList.size() - 1)) //If went to end of the list, tell them name not found.
                    cout << "There is no " << searchedName << " in the list. \n";


        }

        cout << "Would you like to search for another person? \n";
        cout << "Y for Yes, N for No. \n";
        cin >> answer;
        cin.ignore();

    } while (answer == 'Y' || answer == 'y');


    return 0;




}
  • If your program "kind of just crashes," you're in Visual Studio, and you're looking to get ahead of your programming class, then now is a good time to learn how to debug (F5 should start debugging in VS2013) – Foggzie Sep 26 '14 at 01:05
  • Move the `cin.ignore()` to the beginning if the for loop. Read the first part of [this answer](http://stackoverflow.com/a/21567292/701092) for an explanation of what happened – 0x499602D2 Sep 26 '14 at 01:34

1 Answers1

0

You could create a static compare function in class Student to use with std::sort

class Student
{
    string myName, myStudentID, myClassID;
// ...
    static bool compare_name(const Student & lStudent, const Student & rStudent)
        {return (lStudent.myName < rStudent.myName);}
};
// ...
    std::sort(studentList.begin(), studentList.end(), Student::compare_name);

or if your compiler supports lambda functions:

    std::sort(studentList.begin(), studentList.end(),
        [studentList](const Student & lStudent, const Student & rStudent)
            {return (lStudent.myName < rStudent.myName);});
rcgldr
  • 23,179
  • 3
  • 24
  • 50