| | |
Having trouble with rand();
![]() |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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"); }; };
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
Coming from python are we?
In C++, the quotes (") and (') are NOT interchangable.
The following lines in your code:
Generated the following warning in my compiler:
When I changed those lines to use (")
The compiler complained about so I changed it to
This is the output from my sample run:
(I don't like infinite loops, it would have been nice for you to have an actual exit condition.)
In C++, the quotes (") and (') are NOT interchangable.
The following lines in your code:
c++ Syntax (Toggle Plain Text)
choose[0] = 'alpha'; choose[1] = 'list';
C++ Syntax (Toggle Plain Text)
rand.cpp(27) : error C2015: too many characters in constant rand.cpp(27) : error C2593: 'operator =' is ambiguous
The compiler complained about
cout << choose[random]; C++ Syntax (Toggle Plain Text)
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)
cout << choose[random].c_str() This is the output from my sample run:
C++ Syntax (Toggle Plain Text)
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
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). "Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
c++ Syntax (Toggle Plain Text)
#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; } } }
Last edited by Comatose; Jan 10th, 2009 at 4:04 pm.
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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:
The second would be similar, but only use one rand() call:
Is that useful at all to what you're trying to do?
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:
c++ Syntax (Toggle Plain Text)
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:
c++ Syntax (Toggle Plain Text)
char genset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; char generated = genset[rand() % strlen(genset)];
Is that useful at all to what you're trying to do?
![]() |
Similar Threads
- Program for Random numbers having a bit of trouble. (C++)
- Array trouble (C++)
- Having a bit of trouble figuring out my image upload script (PHP)
- trouble with sleeping in an applet (Java)
- Help with rand please. (C++)
- trouble with long output string, plz help (PHP)
- Trouble Converting Variables (C++)
Other Threads in the C++ Forum
- Previous Thread: searching for keywords in multiple text files
- Next Thread: Convert Decimal to hexadecimal number
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






