Programm chrashing

Hi!
I am a bit new to c++ so please bare with me if there are some obvious mistakes.
I am trying to create an programm that takes 9 letters and spits out all words that you can make with these letters. You can only use an letter as many times as given in the beginning. For example the letters could be "a l f a b e t e t" (means alphabet in swedish). In this case you could use the letters a,e and t twice while the other ones only once.
The problem is that when i run the program it just stopps working and i dont know why. Before i programmed it in java but it ran in 20 minutes, by far to slow for my purpose.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <locale.h>
#include <math.h>
#include <algorithm>

using namespace std;

template <class T>
bool contains(vector<T> const &v, T const &x) {
    return ! (v.empty() &&
              find(v.begin(), v.end(), x) == v.end());
}

int main(int argc, char *argv[])
{
    char *locale;

    locale = setlocale(LC_ALL, "");

    if(argc==10) {
        string line;
        ifstream myfile;
        int i,j,k,l,m,n,o,p,q;
        vector<string> dictionary;
        vector<string> used;
        myfile.open("ordlista.txt");
        if(myfile.is_open()) {
            while ( getline(myfile,line)) {
                dictionary.push_back(line);
            }
        }
        myfile.close();

        for (i=1;i<=argc;++i) {
            string word = argv[i];
            if(!(contains(used,word)) && contains(dictionary,word)) {
                cout << word;
                used.push_back(word);
            }
            for (j=1;j<=argc;++j) { //start Letter 2
                if(i!=j) { // start if 2
                    word = string(argv[i]) + argv[j];
                    if(!(contains(used,word)) && contains(dictionary,word)) {
                        cout << word;
                        used.push_back(word);
                    }
                    for (k=1;k<=argc;++k) { //start letter 3
                        if(k!=j&&k!=i) { // start if 3
                            word = argv[i] + string(argv[j]) + argv[k];
                            if(!(contains(used,word)) && contains(dictionary,word)) {
                                cout << word;
                                used.push_back(word);
                            }
                            for (l=1;l<=argc;++l) { // start letter 4
                                if(l!=k&&l!=j&&l!=i) { // start if 4
                                    word = argv[i] + string(argv[j]) + argv[k] + argv[l];
                                    if(!(contains(used,word)) && contains(dictionary,word)) {
                                        cout << word;
                                        used.push_back(word);
                                    }
                                    for (m=1;m<=argc;++m) { // start letter 5
                                        if(m!=l&&m!=k&&m!=j&&m!=i) { // start if 5
                                            word = string(argv[i]) + argv[j] + argv[k] + argv[l] + argv[m];
                                            if(!(contains(used,word)) && contains(dictionary,word)) {
                                                cout << word;
                                                used.push_back(word);
                                            }
                                            for (n=1;n<=argc;++n) { // start letter 6
                                                if(n!=m&&n!=l&&n!=k&&n!=j&&n!=i) { // start if 6
                                                    word = string(argv[i]) + argv[j] + argv[k] + argv[l] + argv[m] + argv[n];
                                                    if(!(contains(used,word)) && contains(dictionary,word)) {
                                                        cout << word;
                                                        used.push_back(word);
                                                    }
                                                    for (o=1;o<=argc;++o) { // start letter 7
                                                        if(o!=n&&o!=m&&o!=l&&o!=k&&o!=j&&o!=i) { // start if 7
                                                            word = string(argv[i]) + argv[j] + argv[k] + argv[l] + argv[m] + argv[n] + argv[o];
                                                            if(!(contains(used,word)) && contains(dictionary,word)) {
                                                                cout << word;
                                                                used.push_back(word);
                                                            }
                                                            for (p=1;p<=argc;++p) { // start letter 8
                                                                if(p!=o&&p!=n&&p!=m&&p!=l&&p!=k&&p!=j&&p!=i) { // start if 8
                                                                    word = string(argv[i]) + argv[j] + argv[k] + argv[l] + argv[m] + argv[n] + argv[o] + argv[p];
                                                                    if(!(contains(used,word)) && contains(dictionary,word)) {
                                                                        cout << word;
                                                                        used.push_back(word);
                                                                    }
                                                                    for (q=1;q<=argc;++q) { //start letter 9
                                                                        if(q!=p&&q!=o&&q!=n&&q!=m&&q!=l&&q!=k&&q!=j&&q!=i) { // start if 8
                                                                            word = string(argv[i]) + argv[j] + argv[k] + argv[l] + argv[m] + argv[n] + argv[o] + argv[p] + argv[q];
                                                                            if(!(contains(used,word)) && contains(dictionary,word)) {
                                                                                cout << word;
                                                                                used.push_back(word);
                                                                            }
                                                                        } //end if 9
                                                                    } //end letter 9
                                                                } //end if 8
                                                            }//end Letter 8
                                                        } // end If 7
                                                    } //end letter 7
                                                }//end if 6
                                            } //end letter 6
                                        } //end if 5
                                    } // end Letter 5
                                } //end if 4
                            } //end Letter 4
                        } // end if 3
                    } //end Letter 3
                } //end If 2
            } //end Letter 2
        } //end Letter 1
    } else {
        cout << "To many or to few arguments";
    }
    return 0;
}
Last edited on
closed account (48bpfSEw)
wow! nice formating! ^^

so far I understood... you need a permutation algorithm to do the job!

http://www.cplusplus.com/reference/algorithm/next_permutation/

take a list of indexes of your given string e.g. "test" -> {0,1,2,3 }

permutate this numbers
result is e.g.

3,2,1,0 -> access to your string with this sequence word[3], word[2], word[1], word[0]
2,3,0,1 -> access to your string with this sequence word[2], word[3], word[0], word[1]



other algorithms here: http://www.cut-the-knot.org/do_you_know/AllPerm.shtml

Last edited on
Ok thanks.
I haven't had the time to test the links but i'll come back when i tried it.
I have now had the time to read through the links you gave me and they are close to what i need. The only problem though is that i need the subwords as well so words that don't use all letters supplied.
Is there any way that i missed to get them?

And thanks again for quick reply
closed account (48bpfSEw)
Can you give an example?
What kind of example?
Topic archived. No new replies allowed.