This is an attempt at porting VB code, here's the VB code and my attempt at the C++. It does compile but not doing the job.
Code:
Private Sub command1_Click()
BOOLER = False
GETALLCOMBOZ charz, 3 ' 4 = number of character combinations to try
End Sub
Public Sub GETALLCOMBOZ(ByVal charact As String, _
ByVal num As Byte)
ReDim myarray(num)
Do While Not (BOOLER)
myarray(numofperms) = myarray(numofperms) + 1
If numofperms = num Then
comboz = Left$(comboz, num - 1) + Mid$(charact, myarray(numofperms), 1)
Else
comboz = comboz + Mid$(charact, myarray(numofperms), 1)
End If
If myarray(numofperms) <> Len(charact) + 1 Then
If numofperms <> num Then
numofperms = numofperms + 1
Else
Print #1, comboz
End If
Else
If numofperms = 1 Then
BOOLER = True
Else
myarray(numofperms) = 0
numofperms = numofperms - 1
comboz = Left$(comboz, numofperms - 1)
End If
End If
Loop
Close #1
End Sub
Code:
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
using namespace std;
char *GetAllCombinations(string charact,int num);
int main(){
GetAllCombinations("abcd",3);
// for example aaa, aab, aac, aad, aba, aca, ada, baa, caa, daa
}
char *GetAllCombinations(string charact,int num){
// redim array to size num
char *myArr = new char[num];
// init vars
int numofperms = 0;
string comboz;
bool booler = false;
// open file for writing too
ofstream a_file ( "c:\\windows\\desktop\\comboz.txt" );
// start combinations loop
do {
myArr[numofperms] = myArr[numofperms]+ 1;
if (numofperms == num)
{
comboz = comboz.substr(0, num - 1)+ charact.substr(myArr[numofperms], 1);
}
else
{
comboz = comboz + charact.substr(myArr[numofperms], 1);
}
if(myArr[numofperms] != charact.length() + 1)
{
if(numofperms!= num)
{
numofperms = numofperms + 1;
}
else
{
cout << comboz;
a_file<< comboz ;
}
}
else
{
if (numofperms == 1)
{
booler = true;
}
else
{
myArr[numofperms] = 0;
numofperms = numofperms - 1;
comboz = comboz.substr(0, numofperms - 1);
}
}
}while (booler != true);
a_file.close();
}
If anybody could work out whats wrong with my C++ then cheers.