0

I'm having some trouble with an assignment of mine. I have a string array with 30 words. I have to combine three of those words to form a password combinations. Combinations such as "airairair" or "allallall" are acceptable. I know that I should have 27000 combinations(30 * 30 * 30), but so far I'm only getting 900. Can somebody help me with this? What am I doing wrong? Code below:

#include<iostream>
#include<string>
#include<list>
#include<fstream>
using namespace std;

int main(int argc, char **argv[])
{
    string passwords[] = { "air", "few", "all", "one", "cot", "jam", "sip", "gag", "arc", "egg",
        "had", "hut", "tan", "paw", "pay", "got", "get", "pea", "rig", "cop",
        "sat", "two", "who", "six", "sow", "dam", "tip", "lit", "awl", "dew" };

    static int numWords = 0;
    static int count = 0;
    int arrLength = sizeof(passwords) / sizeof(passwords[0]);

    ofstream f("passwords.dat"); //write passwords to file
    int i, j, k;

    for (i = 0; i < arrLength; i++)
    {
        for ( j = 0; i < arrLength; i++)
        {
            for ( k = 0 ; k < arrLength; k++)
            {
                cout << passwords[i] << passwords[j] << passwords[k];
                f << passwords[i] << passwords[j] << passwords[k] << "\n";
            }
        }
    }
    f.close();
    system("pause");

    return 0;
}
inspiration
  • 176
  • 3
  • 10

1 Answers1

3

Your J loop is wrong . It should be for( j=0 ; j < arrLength; j++) .

It is however typed wrongly as for( j=0 ; i < arrLength ; i++ )

hence it will give only 30*30 combinations instead of 30*30*30

Shankar
  • 417
  • 1
  • 6
  • 17
  • 1
    Whoops..my bad. I changed it, but now I'm getting a access violation exception. Any ideas why? – inspiration Nov 28 '14 at 23:09
  • 1
    this is wrong: `int main(int argc, char **argv[])` should be `char**argv` or `char*argv[]`. When I fixed that it compiled and worked for me. – Richard Hodges Nov 29 '14 at 00:54
  • I have corrected your entire code . Have a look at this link http://ideone.com/f8MX9h – Shankar Nov 30 '14 at 06:49
  • I suggest you to read this http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong . It is about the use of system("pause"); – Shankar Nov 30 '14 at 06:53