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:46

what a stupid idea. anyway just find a way of separating strings like with a :: or something and read in the file to a std::string, splitting it at each :: to get an array of quotes.

Name: Anonymous 2008-09-10 14:54

>>2
And keep the whole file in memory? You're an idiot.

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.

Name: Anonymous 2008-09-10 15:10

Name: Anonymous 2008-09-10 15:56

well, as for it being a stupid idea, this is A) my first experience with C++, and B), my best friend asked me to make one for her, so it's not for me.

Name: Anonymous 2008-09-10 16:13

Weird how people can work the fucked up Windows GUI APIs but totally fail at simple generic problems.

Troll detected.

Name: Anonymous 2008-09-10 16:32

>>4

#define QUOT_DELIM '~'
/* caller frees! */
char *get_quote(char *fname) {
 FILE *infile;
 char *str;
 int i,c;
 int idx = rand()%NUM_LINES;
 int str_used=0,str_size=64;
 if(!(infile=fopen(fname,"rb"))||!(str=malloc(str_size)))
  return 0;
 for(i=0;i<idx;i++) {
  while((c=fgetc(infile))!=QUOT_DELIM && c!=-1)
   ;
  if(c==-1) {
   free(str);
   return 0;
  }
 }
 do {
  if(str_used>=str_size-1) {
   char *new_str;
   if(!(new_str=realloc(str,2*str_size))) {
    free(str);
    fclose(infile);
    return 0;
   }
   str = new_str;
   str_size *=2;
  }
  c = fgetc(infile);
  str[str_used++]=c;
 } while(c!=QUOT_DELIM && c!=-1);
 str[str_used-1]=0;
 fclose(infile);
 return str;
}


Not tested, written in about 2 minutes.

Name: Anonymous 2008-09-10 16:35

>>8
20s/str_size-1/str_size/

Name: Anonymous 2008-09-10 17:44

>>8
Back to /anonix/, please

Name: Anonymous 2008-09-10 17:55

my best friend asked me to make one for her
Which is the only reason you want to make it.

Name: Anonymous 2008-09-10 18:44

>>1
I ves vundereeng vhet iferyune-a thuooght ooff vreeting a rundum qooute-a generetur in C++ fur XP/Feesta.  I'm hefeeng a herd time-a veet it.  I vunted it nut tu hefe-a a regooler GOoI, joost hefe-a a system trey icun thet vuoold pup up a messege-a boobble-a veet zee rundum qooute-a frum a .txt feele-a, sey ifery huoor oor su.  I theenk I feegoored oooot zee boobble-a theeng, boot getting zee prugrem tu teke-a a rundum qooute-a frum zee text feele-a is dreefing me-a crezy.  Du I hefe-a tu meke-a zee qooutes in seperete-a streengs in zee cude-a ooff zee prugrem, su I cun get zeem tu pup up ifery huoor in a boobble-a veendoo?

Name: Anonymous 2008-09-10 19:47

>>1
Sounds like a job for Markov chains.

Name: RAW MEME FAN 2008-09-10 19:50

Sounds like a job for Markoff Chaney.

Name: Anonymous 2008-09-11 22:50

>>10
What gave it away?

Name: Anonymous 2008-09-11 23:58

here's the web-sites equation:  <span style="font-weight: bold; font-size: 120pt; font-family: Arial, sans-serif; text-decoration: none; color: black;"  >NO</span>

lol.

oh and google anayltics.

Name: Anonymous 2008-09-12 2:18

Hold qoutes in a file, sepearated by newlines.
When yuo need a qoute:
1 - Open file
2 - fseek to random position in file
3 - fgetc until you get a newline
4 - fgets; this will be your quote

Random, but not with same probability for all lines. But you don't need to store what line starts where in file.

Name: Anonymous 2008-09-12 2:18

Oh, and >>4 is a faggot

Name: Anonymous 2009-03-06 5:55

The video Rickroll here.

Name: Anonymous 2009-03-06 6:06

/ eax 2 int   y2 int z2.

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