![]() |
| ||
| Having trouble with rand(); #include <iostream>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. |
| ||
| Re: Having trouble with rand(); Coming from python are we? In C++, the quotes (") and (') are NOT interchangable. The following lines in your code: choose[0] = 'alpha';Generated the following warning in my compiler: rand.cpp(27) : error C2015: too many characters in constantWhen 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 . . . (I don't like infinite loops, it would have been nice for you to have an actual exit condition.) |
| ||
| Re: Having trouble with rand(); 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). |
| ||
| Re: Having trouble with rand(); 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. |
| ||
| Re: Having trouble with rand(); 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. |
| ||
| Re: Having trouble with rand(); #include <iostream> |
| ||
| Re: Having trouble with rand(); 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"; The second would be similar, but only use one rand() call: char genset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Is that useful at all to what you're trying to do? |
| All times are GMT -4. The time now is 12:52 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC