Nickelxk5
07-02-2010, 05:10 PM
Hi guys, I was trying to make a program the allows a user to enter a string, and get every possible arrangement of those letters. I used the random_shuffle() function from algorithms to shuffle the letters around in my string array. then, i created a vector that holds all the random shuffled up strings. But before it enters the string into the vector, i created a function, indexof, to check the contents of the vector to confirm that word would not be repeated. The problem i am having is that the program will bug up when it reads in spaces. I have went through and manually set break points and i have seen that the variable holding my string has the space, but im unclear as to why it bugs up. I just need another eye thanks, here's all the code.
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
void pVector(vector<string> print);
int indexof(vector<string> vec, string word);
long int factorial(int n);
ofstream wFile;
ifstream rFile;
int main() {
wFile.open("scramble.txt");
rFile.open("scramble.txt");
vector<string> print;
string name;
getline (cin, name);
int long letters = name.length();
letters = factorial(letters);
while (print.size() < letters){
random_shuffle(name.begin(), name.end());
if( indexof(print, name) == -1){
print.push_back(name);
}
}
pVector(print);
wFile.close();
rFile.close();
system("pause");
}
void pVector(vector<string> print){
for (int i = 0; i < print.size(); i ++){
wFile << print.at(i) << endl;
}
}
int indexof(vector<string> vec, string word){
for (int i = 0; i < vec.size(); i++){
if(vec.at(i) == word)
return i;
}
return -1;
}
long int factorial(int num){
int result;
if (num <= 1) return 1;
result = num * factorial(num - 1);
return result;
}
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
void pVector(vector<string> print);
int indexof(vector<string> vec, string word);
long int factorial(int n);
ofstream wFile;
ifstream rFile;
int main() {
wFile.open("scramble.txt");
rFile.open("scramble.txt");
vector<string> print;
string name;
getline (cin, name);
int long letters = name.length();
letters = factorial(letters);
while (print.size() < letters){
random_shuffle(name.begin(), name.end());
if( indexof(print, name) == -1){
print.push_back(name);
}
}
pVector(print);
wFile.close();
rFile.close();
system("pause");
}
void pVector(vector<string> print){
for (int i = 0; i < print.size(); i ++){
wFile << print.at(i) << endl;
}
}
int indexof(vector<string> vec, string word){
for (int i = 0; i < vec.size(); i++){
if(vec.at(i) == word)
return i;
}
return -1;
}
long int factorial(int num){
int result;
if (num <= 1) return 1;
result = num * factorial(num - 1);
return result;
}