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:
Anonymous2008-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:
Anonymous2008-09-10 14:54
>>2
And keep the whole file in memory? You're an idiot.
Name:
Anonymous2008-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.
my best friend asked me to make one for her
Which is the only reason you want to make it.
Name:
Anonymous2008-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?
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.