Hi. I am new to C++ and I want to pick out random words and store them in a variable. Any ideas?
Thanks,
Adam
Hi. I am new to C++ and I want to pick out random words and store them in a variable. Any ideas?
Thanks,
Adam
select a file, such as the Declaration of Independence, or the Constitution, or War & Peace, generate a random number and read that word number from the file. Read the file one word at a time until the desired word number is read.
Random words from what? A file? An array? The most effective way to do it depends on where the words are stored when you take a random sample.
Just because I don't care doesn't mean I don't understand.
You could do this using C-strings or STL strings (the string class). But since I don't know what method of strings you've learned, I'll tell you how to do this using C-strings. It very well may be easier with STL strings, and someone else can help you out with that more than I can.
First, you need to read in the FAQ about how to generate random numbers. Then, you can use a function in <cstring> called strtok() that separates a string into different sections based on a delimiting character you specify. Since you're counting words, you would use a space as your delimiter. You would use strtok() in a loop to 'tokenize' the whole string. strtok() returns a pointer to the section it just tokenized, so you would assign these pointers to an array of char *'s. Use your new random-number generating abilities to pick out certain words from this array, and then store them any way you wish.
It would be prefferable to select the word from an array.
>> It would be prefferable to select the word from an array.
And these words get into this array how? If they are in an array already (char *[], std::string []) then you can simply index your someArray[rand() % ARRAY_LIM] ARRAY_LIM being the size of your array.
Edit: >> select a file, such as the Declaration of Independence, or the Constitution, or War & Peace, generate a random number and read that word number from the file. Read the file one word at a time until the desired word number is read.
Is that your assignment that you posted under an alterate account ?
Can I just pull it out of an enumerated list?
Why not:
Maybe is that what you mean, I don't now but I hope this helps you.Code:char String[100]; srand( ( unsigned ) time ( NULL ) ); Random = rand() % 10; switch ( Random ){ case 0 : strcat ( String, "word0" ); break; case 1 : strcat ( String, "word1" ); break; case 2 : strcat ( String, "word2" ); break; case 3 : strcat ( String, "word3" ); break; case 4 : strcat ( String, "word4" ); break; case 5 : strcat ( String, "word5" ); break; case 6 : strcat ( String, "word6" ); break; case 7 : strcat ( String, "word7" ); break; case 8 : strcat ( String, "word8" ); break; case 9 : strcat ( String, "word9" ); break; default : strcat ( String, "word10" ); break;
Hey Ancient Dragon,
how can I choose a random word from a file,
by the way, thanks Yuri, i'll try that.
-Adam
Hey Yuri,
My compiler found a bug in this line:
and this line:Code:Random = rand() % 10;
As I said before, I am new in C++ and am quite helpless in the face of a bug.Code:case 0 : strcat ( String, "word0" );
Do you know what is wrong with it?
P.S. How do I print out my results? eg. Is Random the variable?
Thanks alot,
Adam
Adam
beanroaster: I recommend you continue with the book or tutorials or whatever you are learning C++ from before continuing with this project. You're basically asking us to provide you source code based on very few details, and you have trouble understanding that source code.
My book doesn't have randomization in it. Can you reccomend any tutorials with that in it?
Otherwise if you need more details, I am trying to generate random sentances using all random words that are pulled from seperate arrays. In the end I would want to be able to simply call down the variables like this:
etc...Code:cout << word1 << " " << word2 << " " << word3 " "
I see your point though.
Thank you for your help,
Adam
Originally Posted by beanroasterhttp://faq.cprogramming.com/cgi-bin/...&id=1043284385Originally Posted by sean_mackrory
Do you know how to open a file and read it? If not you should study that first. Basically this is how. NOTE: I did not compile or test this program.Originally Posted by beanroaster
Code:#include <ctime> #include <fstream> #include <string> #include <iostream> using namespace std; int main() { string word; // generate random seed srand( time(0) ); // select a random number int num = rand(); // open the file ifstream in("filename.txt"); if( in.is_open() ) { int i = 0; while( !in.eof() && i != num) { in >> word; ++i; } // did we fine the right word? if( i != num ) { // not that many words in the file. so just bail out cout << "only " << i << " words in the file." << endl; } else { cout << "Word number " << i << " is " << word << endl; } return 0; }
Last edited by Ancient Dragon; 09-03-2005 at 07:10 PM.
thanks
Adam