Thread: Fortune Program Help

  1. #1
    Registered User
    Join Date
    Mar 2002

    Question 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;

    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


    edit:: oops, I forgot to tell you how the data file is set up.....
    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


    Thanks again
    Last edited by ampalfe; 05-01-2002 at 11:11 AM.

  2. #2
    I'm Back
    Join Date
    Dec 2001
    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
    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.

  3. #3
    Registered User
    Join Date
    Mar 2002
    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];

  4. #4
    Registered User
    Join Date
    Mar 2002
    Whew.... Well thanks... I have a lot of changing to do.
    Thanks for your help... I'll post an update when I fix it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. help with small fortune program
    By mackieinva in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 03:55 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM