Functions

/tmp/buildd/witty-3.1.2/examples/hangman/Dictionary.C File Reference

#include <Wt/WStringUtil>
#include "Dictionary.h"
#include <fstream>
#include <iostream>
#include <time.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

std::wstring RandomWord (Dictionary dictionary)

Function Documentation

std::wstring RandomWord ( Dictionary  dictionary  ) 

Definition at line 15 of file Dictionary.C.

{
   std::ifstream dict;
   if(dictionary == DICT_NL) {
      dict.open("dict-nl.txt");
   } else { // english is default
      dict.open("dict.txt");
   }
      
   std::string retval;
   int numwords = 0;
   while(dict) {
      getline(dict, retval);
      numwords++;
   }
   dict.clear();
   dict.seekg(0);

   srand(time(0));
   int selection = rand() % numwords; // not entirely uniform, but who cares?

   while(selection--) {
      getline(dict, retval);
   }
   getline(dict, retval);
   for(unsigned int i = 0; i < retval.size(); ++i)
      if(retval[i] < 'A' || retval[i] > 'Z')
         std::cout << "word " << retval << " contains illegal data at pos " << i << std::endl;

   return Wt::widen(retval);
}


Generated on Mon Nov 29 2010 08:03:14 for Wt by doxygen 1.7.1