I need to pick m amount of random characters(letters) without repetition and im completely stuck, i keep getting only 1 random letter. How can i fix my code? Is there even a way to fix this or should i just scrap this idea and look for a solution from some kinf od tutorials?
#include <iostream> #include <ctime> #include <cstdlib> #include <string> using namespace std; int main() { cout << "number below 27" << endl; int m; cin >> m; srand(time(NULL)); bool repeat = false; char letters[m]; char letter; for(int i = 0; i < m; i++){ letter = rand()%26 +97; repeat = true; for(int j = 0; j < m; j++){ if(letters[m] == letters[j]){ repeat = false; break; } } if(repeat){ letters[m] = letter; } } for (int i = 0; i < m; i++){ cout << letters[m]; } }
char letters[m];VLAs aren't standard c++. Usestd::vector<char> letters(m);instead.