-
Fortune Program Help
Ok. I am writing a program that tells your fortune.
What it does is a random number generator makes a number, then from a data file full of fortunes, it picks the line corresponding with the number. What I can't get it to do is to tell the function for reading the file (ReadFile) what the random number is.
Here is my code:
fyi: I'm programming this in textbased telnet linux.
#include <iostream.h>
#include <fstream.h>
#include <time.h>
#include "home/ampalfe/APClasses/apstring.h"
// Global Constants
const int NUM = 0;
// Function Prototypes
int Numbergenerator();
apstring ReadFile ( NUM );
int main()
{
cout<<" ----The Super Fortune Teller----"<<endl<<endl;
cout<<" warning: all fortunes generated that come true are purely
coincidental and do not reflect the ability of our staff
or this generator."<< endl;
cout<<" Now.... To recieve your fortune<<endl<<endl<<endl<<endl;
cout<< "This is your fortune.........."<<endl;
Numbergenerator();
ReadFile( NUM );
}
int Numbergenerator()
{
srand ( unsigned (time(0)));
NUM = rand()%2;
return NUM;
}
apstring ReadFile(NUM)
{
ifstream inFile("/home/ampalfe/Game/Fortunes.stuff");
getline( inFile, NUM);
return apstring;
}
any help would be apreciated. Thanks
ampalfe
edit:: oops, I forgot to tell you how the data file is set up.....
it's
1 You will eat a lot today
2 You will be attracted to a co worker today
3 don't go outside, the weather is bad
etc.
Thanks again
-
you are returning the value of NUM in the function int Numbergenerator(), but to what also why is NUM a const.
i think NUM should be plain int and in main it should be
Code:
NUM=Numbergenerator();
as for your Q on reading the file i havent used apstring.h (dont have it) so what i will do is search for the particular number in the file and display all the contents from there on till nextline is encountered.
-
place NUM in main() and don't declare it const, otherwise the random number generator is worthless.
the line call the random number generator should be :
NUM = Numbergenerator();
unless you change the prototype for Numbergenerator and pass NUM as an argument by reference in which case the return type of void is probably more appropriate.
put this line back in main(), towards the beginning, preferably as the first line of the body of main().
srand ( unsigned (time(0)));
rand() % 2; will only generate numbers 0 and 1. you want to replace 2 by one number less than the number of fortunes stored in the file.
#include <apvector.h> and declare a vector of strings to hold all the contents of the file. One fortune per string which is the same as one fortune per element of the vector.
drop the leading integer in each of the strings in the file. You can use the vector element index instead. It will be easier. If you must use the integer value you will have to separate the integer value from the string holding the fortune through whichever means you like.
use a loop to read all fortunes from file into vector one fortune/string at a time.
then use NUM (the random number you generated) as the vector index to display fortune like this:
cout << vectorName[NUM];
-
Whew.... Well thanks... I have a lot of changing to do.
Thanks for your help... I'll post an update when I fix it.