is it not possible to randomize already existing variables?
char a="hi";
char b="I am";
char c="new to this";
?
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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; }
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
Last edited by matsp; 01-01-2009 at 02:01 PM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
thanx for the replies.
I did load them into a array and then randomized them.
worked wonders