Random Word Generator?

Started by
11 comments, last by Ekim_Gram 20 years, 8 months ago
For selecting words just do something simple like so:
#include <iostream>#include <fstream>#include <vector>#include <string>#include <iterator>#include <algorithm>#include <ctime>#include <cstdlib> int main(){    std::ifstream in_file("words.txt");    if ( in_file.fail() )    {        // handle error    }     std::vector<std::string> v_words;    std::copy( std::istream_iterator<std::string>(in_file),               std::istream_iterator<std::string>(), std::back_inserter(v_words) );     srand( unsigned ( time(0) ) );   // seed rand()    std::string word = v_words[ rand() % v_words.size() ];     // now ''word'' is a word randomly selected from your word list     ...     // rest of program     ...     return 0;}

You will have to learn some STL stuff; fstream and std::vector in particular.

pkelly83: People have to start somewhere. Drop the arrogance.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Advertisement
Lektrix: okay okay sorry.

Just feeling hyperactive and mad and high and shit. No mean to offend.
Good, then I shalln''t have to castrate you. :]

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement