Hey guys :) i'm new at programming in C++ and i'm stuck at an assignment that im suppose to write

I need to Write a string function that gets a word at random from a file and returns it as the value of the function. It should use a random number generator to get a random number in the range 1 to number of words in the file, then open the file and read in that many words and then return the last word that was read in.

the file is composed of an integer on top that says how many words that are in the file and the same integer is the max range for the random generator that will tell the string function how many words to read in .

I know how to make the number generator but I'm not sure how to make the string function read in the words

an example of the txt file
6
amadeus
toupet
kiley
liope
limouri
getsuga

can only use fstream , iostream libraries
and no arrays :(

i hope someone can help me ... thank you :)

Did you miss the sticky post entitled We only give homework help to those who show effort?
Or maybe the one titled Read This Before Posting?
What about The Rules?

im not asking for anyone to do my homework for me I'm trying to learn , i know how to read in the strings and to create the number generator its just that I'm not sure if what i had was right that's all but never mind then thanks again :)

The point is, show the code you've written, and if you have some idea of what part's giving you problems, point that out. Then you'll find many people here will assist you in solving your problem.

The point is, show the code you've written, and if you have some idea of what part's giving you problems, point that out. Then you'll find many people here will assist you in solving your problem.

thank you i'll edit my main post and sorry if I broke any rules

This is what I have so far

// string function that returns words picked by program
string get_word (int seed1)
{
    ifstream infile ;// file input 
    string word ; //word to be read in 
    int ct = 0 ; //counter for loop
    int seed1 , randomNum ;

    infile.open("input.txt");
    infile >> seed1; 
    srand(seed1) ;
    randomNum = rand()% seed1 + 1; 


    while (infile && ct < randomNum)
    {
        infile >> word;
        ct++ ;
    }

    return word ; 
}

Looks pretty good except that you are defining an integer with the same name as the parameter passed to it. I doubt I would pass anything to the function. I don't see that you use the number passed to it or that you need anything passed to it. I would probably seed the random number generator first thing in main with time (0) instead of in the function.

I'd do the loop as

while ( ct < randomNum && infile >> word)
	{
		ct++ ;
	}

It shouldn't make any difference in this particular program, but it's a method that will be more resilient in future things you do. Establish good habits early.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.