is it not possible to randomize already existing variables?
char a="hi";
char b="I am";
char c="new to this";
?
Printable View
is it not possible to randomize already existing variables?
char a="hi";
char b="I am";
char c="new to this";
?
Your question doesn't really make that much sense.
What is the actual problem you are trying to solve?
[I can think of at least three different things that you MAY mean - but I'm not going to post any of them, as I'd probably get it wrong anyways].
--
Mats
I got X number of different variables...all decleared with Char.
what I want is to "cout << variablex"
so each time the program starts it will send 1 random of the variables to the screen, then shut down
.
sry if that didn`t make much sense
Declare an array of them.
Then generate a random subscript.
so you said you have x char variables, and each time the program runs, you want one of the x char variables to print, and then exit?is this logic something that your looking for? again, to me this seems like what you are askingCode:int main()
{
char myChars[] = { 'a', 'b', 'c', 'd' };
// seed random number generator
int random = // call function to generate random number from 0 to 3 inclusive
cout << myChars[random] << endl;
}
Also use const char* for string constants or (const) std::string.
You can't do THAT, but as suggested elsewhere, you can use an array and a random index to achieve the effect you want. But what you suggest involves runtime decisions on which variable to use, and the C or C++ languages do not support that.
Edit: And of course, if you are REALLY desperate to not create an array of your variables, you could make an array of pointers to the original variables, e.g.
--Code:char *a = "Hi";
char *b = "Hello";
char *c = "'Morning";
char **arr[] = { a, b, c };
...
Mats
thanx for the replies.
I did load them into a array and then randomized them.
worked wonders :)