All pastes #894641 Raw Edit

Untitled

public text v1 · immutable
·published 2008-02-07 03:27 UTC
rendered paste body
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <fstream>

std::string randWord( std::vector<std::string> inVector );
std::vector<std::string> createDictionaryVector( std::ifstream &inFile );

int main() {
  std::ifstream dictionary;
  dictionary.exceptions( std::ifstream::failbit | std::ifstream::badbit );

  try {
    dictionary.open("/home/nickpresta/words");
  } catch ( std::ifstream::failure e ) {
    std::cerr << "Error opening file. Ending..." << std::endl;
    exit(1);
  }

  std::vector< std::string > dictionaryVector = createDictionaryVector( dictionary );
  std::cout << randWord( dictionaryVector ) << std::endl;

  dictionary.close();
  return 0;
}

std::vector< std::string > createDictionaryVector( std::ifstream &inFile ) {
  std::vector< std::string > dictionaryVector;
  std::string word;
  while ( inFile >> word )
    dictionaryVector.push_back( word );
  return dictionaryVector;
}

std::string randWord( std::vector<std::string> inVector ) {
  std::srand( std::time(NULL) );
  std::string word = inVector[ std::rand() % inVector.size() ]; 
  return word;
}