1

Currently created a piece of code that is able to output from a text file, I am reading the text and the putting each different piece of information into an array.

I have used 4 different arrays, as there is 4 different types of information I want to store. The code for this is working as expected, but I am unsure how I can now sort the information in a way which 1 of the arrays is sorted alphabetically, with all the corresponding arrays to stay in line and be output at the correct time.

void displayfile(){

 string filename1;
 string rartist[NUM];
 string rtitle[NUM];
 string ryear[NUM];
 string rcategory[NUM];
 ifstream openFile;
 int counter = 0;
 int continu
 bool error = false;
 cout << "test";

 do{
     //Loop to retrieve the file name from user, then open file
     do{
        cout  << "Please enter the name of the menu you would like to open: ";
        cin >> filename1;
        filename1 +=  ".txt";
        openFile.open(filename1.c_str());
        if(openFile.fail()){
            cerr << "Check spelling of file name.\n";
            error = true;
        }
    //Storing text from file into arrays
    }while(error == true);
    while(getline( openFile, rartist[counter], ':') && getline( openFile, rtitle[counter], ':') &&
      getline( openFile, ryear[counter], ':') && getline( openFile, rcategory[counter])){
    counter++;
    }
    //outputting the information stored in the array
    cout << "ARTIST   " << "  DVDTITLE    " << "    YEAR    " << "  CATEGORY    \n";
    for(int i = 0; i < counter; i++){
        cout << rartist[i] << "               " << rtitle[i] << "               "
             << ryear[i] << "              " << rcategory[i] << "\n";
    }
    cout << "\n\nIf you would like to read another file, Press 1: ";
    cin >> continu;
  }while(continu == 1)
}

This is the function that I am using to display the text currently.

2
  • 1
    Easiest way is to restructure the data so you have a single array of structures containing the related items, rather than a separate array for each item type. Commented Aug 19, 2016 at 21:59
  • This could be what I need, as I am keeping this function and creating a new function to do the sorting, as another option. Thanks Ill look into this now. Commented Aug 19, 2016 at 22:02

2 Answers 2

3

I assume you are reading pieces of information about songs, and you want to sort them according to the song titles. Since you are reading the same kind of data for each song, use a single array of structures, not separate arrays.

For instance, this is how you would sort the songs by title.

struct Song {
    std::string artist,
    std::string title,
    std::string year,
    std::string category
};

std::vector<Song> songs(NUM);

// Read data

std::sort(songs.begin(), songs.end(),
    [](const Song &a, const Song &b) {
        return a.title < b.title;
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Can I store into the struct the same as storing into separate arrays using the loop?
Of course. Instead of rartist[i], use songs[i].artist.
1

Totally untested c++11 code

std::vector<int> indexes(NUM);
// fill with 0..NUM-1
std::iota(indexes.begin(), indexes.end(), 0);

// example sort after artist.
std::sort(indexes.begin(), indexes.end(),
    [&rartist](const int &lhs, const int &rhs) { 
        return rartist[lhs] < rartist[rhs];
    });
// indexes is now sorted in the same way rartist would have been.

// now iterate in order.
for (int i : indexes) { 
    std::cout << rartist[i] << "              "
              << rtitle[i]  << "              "
              << ryear[i]   << "              "
              << rcategory[i] << "\n";
}

3 Comments

You may be interested by std::iota.
for range seems more natural than std::for_each
@Jarod42, I would not be disinclined to think your right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.