Copying a specific char from one array to another

Hello, all.

As a starting project in C++, I'm attempting to make a program that generates random 14-character codes. Here is the segment I'm most certain is causing problems.
________________________________________________________________________________

//char Alpha[] = {'A', 'B', 'C', ..., 'Z', /0};

//char Code[15]; //These are declared at the beginning of the program.

for(code_count = 0; code_count <= 20; code_count++)
{
for(i = 0; i < 15; i++)
{
choice = rand() % 2;

if(choice == 0)
{
alpha = rand() % 27;

Code[i] = Alpha[alpha]; // ERROR
}

if(choice == 1)
{
number = rand() % 10;

Code[i] = number;
}
}
}
________________________________________________________________________________
Compiling and running with Visual C++ produces a runtime error that causes the resulting characters to be displayed as aces, smiley faces, etc. I have looked at several websites, but this was the format that was used by in copying a specific character from one array to another. Any insight is appreciated.
The code you've show looks fine enough. I would try to narrow the code down and create as small a (complete) program as possible that still reproduces the error to help figure it out.
It took me a while, looking at it to figure it out. But I think your problem is, is that your assigning each char in the array and not having a NULL( \0 ) value at the end when printing it out.

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
int main()
{
	srand( unsigned( time( NULL ) ) );
	
	const char Alpha[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
					 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0' };

	const int max_size = 15;

	char Code[ max_size ] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', '\0' };

	std::cout << "Alpha:\n" << Alpha << "\n\n";
	std::cout << "Code:\n" << Code << "\n\n";

	int choice = 0;
	int alpha = 0;
	int number = 0;

	for( int i = 0; i < max_size; i++ )
	{
		choice = rand() % 2;

		if( choice == 0 )
			{
				alpha = rand() % 26;
				Code[ i ] = Alpha[ alpha ];
			}

		if( choice == 1 )
			{
				number = rand() % 10;
				Code[ i ] = Alpha[ number ];
			}
	}

	Code[ max_size - 1 ] = '\0'; //make the last element = \0 'NULL' as to end the 'cout' of the array
								 //max_size - 1, because when creating an array, elements are [ n - 1 ].
								 // array[ 10 ] = 0 through 9
	std::cout << "Random code:\n" << Code << '\n';

	return 0;
}



EDIT:
Also, all I could see this code doing, was looping 20 times, and re-defining the random Code, 20 times!
for(code_count = 0; code_count <= 20; code_count++)
Last edited on
gj, Lynx876

I would make a few small mods, though

1. Line 8: change 30 to 15 for a 14-character code
2. making assumptions from the variable names and the initial coding, I would change Line 32 to:
Code[ i ] = '0' + number;

otherwise looks pretty good to me (haven't compiled/run tho)
Topic archived. No new replies allowed.