cppcoder 0 Newbie Poster

Hi there everybody just needed some help sorting an object array. So the array takes in a title, link, comment, length, and rating of the video. Once the values are put into the array the user inputs whether they want to sort it by title(alphabetically), length of the video (ascending order), and then rating of the video (descending order). The code runs and compiles perfectly fine right now but just dont know how to put in the place the sort functions.

VIDEO.H

#ifndef VIDEO_H
#define VIDEO_H

class Video{
public:
    Video(string t, string l, string c, float time, int a);
    string getTitle(string t);
    string getLink(string l);
    string getComment(string c);
    int getLength(float hour);
    int getAsterisk(int a);
    string printAsterisk(int a);
    void print();
private:
    string title;
    string link;
    string comment;
    float length;
    int asterisk;

};

#endif

VIDEO.CPP

#include<iostream>
using namespace std;
#include "video.h"

Video::Video(string t, string l, string c, float hour, int a){
    title = t;
    link = l;
    comment = c;
    length = hour;
    asterisk = a;

}
string Video::getTitle(string t){
    title = t;
    return title;
}
string Video::getLink(string l){
    link = l;
    return link;
}
string Video::getComment(string c){
    comment = c;
    return comment;
}
int Video::getLength(float hour){
    length = hour;
    return length;
}
int Video:: getAsterisk(int a){
    asterisk = a;
    return asterisk;
}
string Video::printAsterisk(int a){
    asterisk = a;
    int counter = 0;

    const string starz[6] = {".", "*", "**", "***", "****","*****"};
    for (int i =0; i < asterisk; ++i) {
        ++counter;
    }
    return starz[counter];
}

void Video::print(){
    cout << title << ", " << link << ", " << comment << ", " << length << ", " << printAsterisk(asterisk) << endl;
}

MAIN CPP

#include <iostream>
#include<string>

using namespace std;

#include "video.h"

int main()
{
    const int MAX = 100;
    string title;
    string link;
    string comment;
    float length;
    int rating;
    string sortMethod;
    int numElements = 0;

    Video *videoObject[MAX];

        cout << "<sort method> (rating, length, or title)" << endl;
        cin >> sortMethod;
        cin.ignore();

    if (sortMethod != "rating" && sortMethod != "length" && sortMethod != "title"){
        cerr << sortMethod << " is not a legal sorting method, giving up" << endl;
        return 1;
    }

    for (int i = 0; i < MAX; ++i) {

        getline(cin,title);
        getline(cin,link);
        getline(cin, comment);
        cin >> length;
        cin >> rating;
        cin.ignore();

    if (i >= MAX) {
        cerr << "Too many videos, giving up." << endl;
        return 1;
    }

    videoObject[i] = new Video(title, link, comment, length, rating);
    ++numElements;

    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.