mercredi 12 août 2020

Why can't I use random_shuffle on this dynamic array?

I am trying to make this program where I read in a file of student names (first name and last name) and then output the students in randomized groups. To accomplish this, I figured that I'd number the students in an array (numArray), then randomly shuffle this array, then output this shuffled array into an output file. Here is my code:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <ctime>
#include <vector>

using namespace std;

int main(){
    ifstream inFile;
    ofstream outFile;

    string fileName;
    string outName;
    int uniqueGroups; // how many unique sets of groups to make

    srand(std::time(0)); // generates truly random seeds every time rand() is called

    cout << "Please enter class file name: ";
    getline(cin,fileName);
    cout << endl;

    inFile.open(fileName);

    cout << "Please enter the output file name: ";
    getline(cin,outName);
    cout << endl;

    outFile.open(outName);

    outFile << fixed << left;
    
    int counter = 0;
    string tmp1, tmp2;
    inFile >> tmp1 >> tmp2;
    while(inFile){
        counter++; // we count up how many students are in the file

        outFile << tmp1 << " " << tmp2 << ": " << counter << endl;
    
        inFile >> tmp1 >> tmp2; // this is needed to keep reading input
    }   

    std::vector<int> numArray(counter); // creates a dynamic array as many students as there are
    
    int i; // for the "for" loop below
    for(i = 1; i <= counter; i++){ // numbers the array
        numArray[i] = i;
    }

    cout << "How many times do you want to shuffle groups? ";
    cin >> uniqueGroups;
    cout << endl;

    int randomArray[counter]={0}; // will store the randomized array; initialized with 0
    int randomNo; // will store the random number

    int j; // for the nested for loop
    for(i = 0; i < uniqueGroups; i++){ // note: we were having problems when compiling the numArray part because
                       // the compiler doesn't know how big numArray will be. It's a dynamics array.
        for(j = 0; j < counter; j++){
            outFile << std::random_shuffle(std::begin(numArray), std::end(numArray)) << endl; // end must be "counter"
        } 
    }   

    inFile.close();
    outFile.close();

    return 0;
}

I have been obtaining the following error for over an hour now, and I have no clue how to resolve the issue.

groupsort.cpp:63:12: error: no match for 'operator<<' (operand types are 'std::ofstream' {aka 'std::basic_ofstream<char>'} and 'void')
    outFile << std::random_shuffle(std::begin(numArray), std::end(numArray)) << endl; // end must be "counter"



Aucun commentaire:

Enregistrer un commentaire