#include #include #include #include #include std::string randWord( std::vector inVector ); std::vector 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 inVector ) { std::srand( std::time(NULL) ); std::string word = inVector[ std::rand() % inVector.size() ]; return word; }