Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

random quote generator

Name: Anonymous 2008-09-10 14:35

I was wondering what everyone thought of writing a random quote generator in C++ for XP/Vista.  I'm having a hard time with it.  I wanted it not to have a regular GUI, just have a system tray icon that would pop up a message bubble with the random quote from a .txt file, say every hour or so.  I think I figured out the bubble thing, but getting the program to take a random quote from the text file is driving me crazy.  Do I have to make the quotes in separate strings in the code of the program, so I can get them to pop up every hour in a bubble window?

Name: Anonymous 2008-09-10 14:59


std::string get_random_quote() {
  std::vector<std::string> quotes;
  std::istream in("reddwarf.txt"); //Available on demand
  in.exceptions(std::ios::failbit | std::ios::badbit);
  while(in) {
    std::string line;
    std::getline(in, line);
    //do some sort of trim() thing here if you like
    if(line.size() > 0)
      quotes.push_back(line);
  }
  //float->int truncates
  int idx = int(float(std::rand())/RAND_MAX*quotes.size());
  return quotes.at(idx);
}


Not really tested.

You could do all sorts of optimisations and crap, but honestly, who cares? It's happening once an hour, it doesn't need to be OMG OPTIMIZED.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List