Combination Generator Help

Hello,

I coded a program to generate combinations of a string using the numbers 0 thru 9 and characters A thru Z and then have them written to a text file. The program works fine except I'm getting a space in the output text file that I don't want and did not intend. Needless to say I cant figure out were the problem is. I am thinking it is something to do with the arraySize variable being set to a value of 37. But if I set it to 36 then I get an "Array bounds overflow" error. I'm fairly new to programming and have not made it through a C++ programming book past working with loops and arrays yet. I could use some help in trying to figure out what I'm missing in the following code. I'm using Microsoft Visual Studio as my IDE.

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
#include<iostream>
#include <fstream>
#include "stdafx.h"
#include<conio.h>

using namespace std;

int main()
{
	ofstream myfile;
	myfile.open("combinations.txt");
	const int arraySize = 37;
	char arrayOne[arraySize] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char arrayTwo[arraySize] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char arrayThree[arraySize] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char arrayFour[arraySize] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char arrayFive[arraySize] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char arraySix[arraySize] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for (int a = 0; a <= arraySize; ++a)
	{
		for (int b = 0; b <= arraySize; ++b)
		{
			for (int c = 0; c <= arraySize; ++c)
			{
				for (int d = 0; d <= arraySize; ++d)
				{
					for (int e = 0; e <= arraySize; ++e)
					{
						for (int f = 0; f <= arraySize; ++f)
						{
							cout << arrayOne[a] << arrayTwo[b] << arrayThree[c] << arrayFour[d] << arrayFive[e] << arraySix[f] << endl;
							myfile << arrayOne[a] << arrayTwo[b] << arrayThree[c] << arrayFour[d] << arrayFive[e] << arraySix[f] << endl;
						}
					}
				}
			}
		}
	}
	myfile.close();
	cout << endl << endl;

	// Finish Up
	cout << "Press any key to exit . . ." << endl;
	_getch();
	return 0;
}
Your arrays are of size 37 but your loops iterate 38 time - from 0 (!) to 37.
Tip:

You don't need 6 different arrays containing the same string. Do declare only one which will be indexed by your different indices a to f.
You may also want to declare the array 'const' like the array size because you never changes its content.
Question though, I don't know how to index the string into each indices of a to f. Sorry if this sounds like a stupid question. I just haven't gotten that far yet into the programming courses and books.
Never mind, I just figured that part out.
1
2
3
const char alphaNums[arraySize] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//...
cout << alphaNums[a] << alphaNums[b] << alphaNums[c] << alphaNums[d] << alphaNums[e] << alphaNums[f] << endl;


Does it answer your question?
Now here is my next question. I keep getting a space in the output file. I cut the program down from 5 nested loops within the first loop to just one for testing purposes.

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
#include<iostream>
#include <fstream>
#include "stdafx.h"
#include<conio.h>

using namespace std;

int main()
{
	ofstream myfile;
	myfile.open("combinations.txt");
	const char array[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	// Loops
	for (int a = 0; a <= 36; ++a)
	{
		for (int b = 0; b <= 36; ++b)
		{
			cout << array[a] << array[b] << endl;
			myfile << array[a] << array[b] << endl;
		}
	}
	myfile.close();
	cout << endl << endl;

	// Finish Up
	cout << "Press any key to exit . . ." << endl;
	_getch();
	return 0;
}



Here is some of the output that I'm getting with the space.

1
2
3
4
5
6
7
8
9
10
ZW
ZX
ZY
ZZ
Z 
 0
 1
 2
 3
 4


I'm not sure what I'm missing in the code causing this space happen. I'm thinking it has to do with my initial string and the declared size of the array. I just haven't figured it out yet.
Last edited on
I have tried a using a variable that is equal to " " (space) but I can't seem to get the IDE to accept it has a valid variable and not get a syntax error. If I can figure this out I know I can put in an if statement that will make the program not write these lines of output to the text file.
Last edited on
A char-array has a special property. It terminates using character '\0' which is invisible and may look like a space on your output. Such an array is called a C-string. In fact the number of relevant characters in your C-string is 36 only. To avoid manual counting letters and digits try the following code:

1
2
const char alphaNums[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const unsigned int alphaNumCount = strlen(alphaNums);


Then use alphaNumCount as termination condition of your loops. (Use '<' instead of '<=').
Cool. Thanks, its working like I want it to now. I appreciate the help.
Topic archived. No new replies allowed.