|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
| Sponsored Links |
|
|
|
|||||
|
First you need to generate the number, which is the hardest part of what you're seeking. Next, it would be the easiest to use an vector, which also have good facilities for adding new words.
I'm unable to make a complete example for you at the moment, but if I get the time, I can make a working example - but first in next week, sorry. |
|
|||
|
One way to do it is have the list stored in a basic array something like this (i am using the string class but you could do this with a 2 dimensional array but thats a pain so if you do it like this you need to #include<string>)
Code:
string words[] = {"cat","dog","door","moon"};
Code:
//This is used to seed the random sequence generated by rand() srand(time(NULL)); //Use the %4 to keep the random number between 0-4 cout << words[rand()%4] << endl; |
|
|||||
|
Your code seemed to work (not 100% sure if it did, but didn't get any errors). Just a question or two about it.
Does putting %4 in there make it so the biggest a word can be is four letters? Also, is there a limit to the amount of words I can enter in the string?
__________________
Never be bullied into silence. Never allow yourself to be made a victim. Accept no one's definition of your life; Define yourself. -Harvey Fierstein |
|
|||
|
Here is a full example that will load the words from a file and then keep them in a vector
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
using std::cout;
using std::fstream;
using std::string;
using std::vector;
bool loadWords(char *,vector<string> &);
int main()
{
srand(time(NULL));
vector<string> wordList;
if(loadWords("C:\\wordFile.txt",wordList))
{
cout << "5 Random words from the file\n";
for(int i=0; i<5; i++)
cout << wordList.at(rand() % wordList.size()) << '\n';
}
else
cout << "There was an error opening the file\n";
return 0;
}
bool loadWords(char * fileName,vector<string> & words)
{
fstream file(fileName,std::ios::in);
char buffer[30];
if(file.is_open())
{
words.clear();
while(!file.eof())
{
file.getline(buffer,30);
words.push_back(buffer);
}
file.close();
return true;
}
else
return false;
}
|
|
|||||
|
As I said, I will come with a full example.
Code:
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <cstdlib>
#include <ctime>
void Wait(int iTime)
{
clock_t End;
End = clock () + iTime;
while(clock() < End);
}
void AddWord(std::vector<std::string> &vList, std::string sWord)
{
std::cout << " - Adding ... \"" << sWord << "\"" << std::endl;
vList.push_back(sWord);
}
std::string GetWord(std::vector<std::string> vList, unsigned int iIndex)
{
return vList.at(iIndex);
}
int GetRandom(int iMax)
{
int iRandom;
Wait(100);
srand(static_cast<unsigned int>(time(NULL) * clock()));
iRandom = (rand() / (RAND_MAX / iMax + 1));
return iRandom;
}
int main()
{
int iSize;
int iRandom;
std::string sWord;
std::vector<std::string> vWords;
std::cout << "Adding 10 words to the list" << std::endl;
AddWord(vWords, "Book");
AddWord(vWords, "Wall");
AddWord(vWords, "Computer");
AddWord(vWords, "Calculator");
AddWord(vWords, "Television");
AddWord(vWords, "Bed");
AddWord(vWords, "Flower");
AddWord(vWords, "Chair");
AddWord(vWords, "Table");
AddWord(vWords, "Cup");
assert(vWords.size() == 10);
std::cout << std::endl;
std::cout << "Generating 5 random words from the list" << std::endl;
for(unsigned int iIndex = 0; iIndex < 5; iIndex++)
{
iSize = vWords.size();
iRandom = GetRandom(iSize);
sWord = GetWord(vWords, iRandom);
std::cout << " - Returned ... \"" << sWord << "\"" << std::endl;
}
return 0;
}
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Generating Random Numbers with PHP | Paradine | PHP Tutorials | 4 | 08-27-2007 08:09 PM |
| Return random numbers without duplicates | Paradine | PHP Tutorials | 0 | 08-26-2007 03:07 PM |
| How do I program a random generator? | eddiewillers | General Programming | 4 | 07-24-2007 03:26 PM |
| Dictonary Program | programmer 101 | Java Help | 9 | 07-01-2007 02:39 PM |
| Code:PHP Random Functions | John | PHP Tutorials | 2 | 12-03-2006 11:04 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |