#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;

int main() 
{
    srand(time(0));
    int number;
    number = 2;
    while(2 == number) {
        int random;
        random = rand() % 2;
        string alpha[3];
        alpha[0] = "a";
        alpha[1] = "b";
        alpha[2] = "c";
        
        string list[3];
        int ran;
        ran = rand() % 3;
        list[0] = "1";
        list[1] = "2";
        list[2] = "3";
        string choose[2];
        choose[0] = 'alpha';
        choose[1] = 'list';
        cout << choose[random];
        
        
        system("PAUSE");
        
        
        
        };
    };

My program as seen above runs just fine for me, except for one small yet crippling bug. The random character that comes out of this program is not from either of my list's! It is always either t or h. Why does this happen? Thanks.

Coming from python are we?

In C++, the quotes (") and (') are NOT interchangable.

The following lines in your code:

choose[0] = 'alpha';
        choose[1] = 'list';

Generated the following warning in my compiler:

rand.cpp(27) : error C2015: too many characters in constant
rand.cpp(27) : error C2593: 'operator =' is ambiguous

When I changed those lines to use (")

The compiler complained about cout << choose[random];

rand.cpp(29) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

so I changed it to cout << choose[random].c_str() This is the output from my sample run:

alphaPress any key to continue . . .
listPress any key to continue . . .
alphaPress any key to continue . . .
listPress any key to continue . . .
listPress any key to continue . . .
alphaPress any key to continue . . .
listPress any key to continue . . .
listPress any key to continue . . . ^C

(I don't like infinite loops, it would have been nice for you to have an actual exit condition.)

Do not declare things in a loop. Do it all before hand.
I think you're confusing std::string with a C style string, because you can just append things to the end without dealing with [] . Unless you plan on allocating them(no need with STL containers like vector).

Murtan, im actually comming from perl, thanks for trying although I wanted the program to choose a random element from one of the two lists. If I had wanted what your version gave me I couldve used the "". The reason I did not include an exit to the loop is because it is a sample program, and it is meant simply for showing you the bug. Once again thanks for trying. Mosaic, thanks for the tip about declaring stuff before the loop, although the second part of your post I believe is directed towards Murtan. Im still tinkering with it to find a solution, if you find one please share.

It was towards you. Because you're trying to create an array of std::strings, which you don't need to. You can just dynamically append() data to a string instead of splitting it over arrays of strings(which you should allocate first). If you need a lot of strings like in choose use a container.

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;

int main() 
{
	int number = 2;
	int random;
	int ran;
	string alpha[3];
	string list[3];
	string choose[2];

	srand(time(0));

	while(2 == number) {
        	random = rand() % 2;
	        string alpha[3];
        	alpha[0] = "a";
	        alpha[1] = "b";
        	alpha[2] = "c";
        
	        ran = rand() % 3;
		list[0] = "1";
	        list[1] = "2";
        	list[2] = "3";
	        choose[0] = "alpha";
        	choose[1] = "list";

		if (choose[random] == "alpha") {
		        cout << "get random alpha" << endl;
			cout << alpha[ran] << endl;
		} else {
			cout << "get random list" << endl;
			cout << list[ran] << endl;
		}
	}
}

It would appear (from the code and our following discussion) that you want to randomly select a character from one of the two lists.

Lets go just a bit higher level, one list appears to be letters and the other appears to be digits.

Are you looking for a 'two-level' selection where we first select letters or digits and then select one of those?

Or would a 'one-level' select one from the set of letters and digits be sufficient?

For the first you could do something like:

char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char digit[] = "0123456789";

char generated;
if (rand() % 2 == 0)
    generated = alpha[rand() % strlen(alpha)];
else
    generated = digit[rand() % strlen(digit)];

The second would be similar, but only use one rand() call:

char genset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char generated = genset[rand() % strlen(genset)];

Is that useful at all to what you're trying to do?

This article has been dead for over six months. Start a new discussion instead.