que me saque la matriz de 10*10 con palabras de un archivo y en los espacios me ponga una letra aleatoria me saca las palabras tal cual, llevo un rato volviéndome loco y no doy con el problema.
Código
#include <vector> #include <iostream> #include <fstream> #include <string> #include <cstdio> #include <cstdlib> #include <ctime> #include <random> using namespace std; string filename = "words.txt"; void error(const string&); vector<string> load_vector(const string& filename) { vector<string> vi; ifstream ifile {filename}; if (!ifile) error("load_vector cannot open the file " + filename); copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi)); return vi; } void error(const string& s) { cerr << "Error: " << s << endl; exit(EXIT_FAILURE); } void aleatorio (int HEIGHT, int WIDTH,const char* puzzle[HEIGHT][WIDTH]) { srand(time(NULL)); //inicializamos semilla string random; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { random = 'A' + (rand() % 26); if (puzzle[i][j] == " ") { puzzle[i][j] = random.c_str(); } } } } char wordToMatrix(int HEIGHT, int WIDTH) { char puzzle[HEIGHT][WIDTH]; int filarand = 0; vector<string> load_vector(const string&); vector<string> words = load_vector(filename); string aux; char empty = ' '; const char* aux2; char random; char espacio = ' '; //Inicializamos matriz for (int i = 0; i < HEIGHT; i++) { cout << endl; for (int j = 0; j < WIDTH; j++) { puzzle[i][j] = '1'; cout << puzzle[i][j]; } } filarand = rand() % 10; //cout << filarand << endl; for (int i = 0; i < 10; i++) { filarand = rand() % 10; cout << "" << endl; for (int j = 0; j < words[i].size(); j++) { if (words[i].size() < 10) { //aux2 = words[i].substr(j,1); aux = words[i].substr(j,1); puzzle[i][j] = aux[0]; cout << puzzle[i][j]; } } } for (int i = 0; i < 10; i++) { //cout << endl; for (int j = 0; j < 10; j++ ) { if (puzzle[i][j] == '1') { random = 'A' + (rand() % 26); puzzle[i][j] = random; //cout << puzzle[i][j]; } } } /*for (int i = 0; i < HEIGHT; i++) { cout << endl; for (int j = 0; j < WIDTH; j++) { random = 'A' + (rand() % 26); if (puzzle[i][j] == espacio) { puzzle[i][j] = random; cout << puzzle[i][j]; } } }*/ return puzzle[HEIGHT][WIDTH]; } int main() { int HEIGHT = 10; int WIDTH = 10; char puzzle[HEIGHT][WIDTH]; srand(time(NULL)); wordToMatrix(10,10); /*for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { cout << puzzle[i][j] << endl; } }*/ return 0; }