Hi,

I'm trying to have a function that takes an ifstream object and returns an int as in int wCount(ifstream myFile); Is that possible?

The compiler gives me loads of errors that I simply don't understand.

Thanks,

You can't copy an istream object, pass a reference instead:

int wCount(ifstream& myFile);

Thanks Narue for the second time today.
Now it turns up I don't know how to pass a reference...

#include<iostream>
#include<fstream>
using namespace std;
int wCount(ifstream & myFile)
{
ifstream elFile(myFile);
// do something

elFile.close;

return anInt;
}

int main()
{
string myFile;
int count= wCount(myFile);
return 0;
}

Is that the correct way to go?
I think I'm going crazy whit this little thing.

Thanks

>Is that the correct way to go?
No, you're passing entirely wrong types and the syntax is broken. What are you trying to do (aside from pass an ifstream reference)?

Oh dear.

I'm trying to have a function that chooses a word from a text file. This function calls another one first that counts the number of words of this text file.
So, what I have:

#include<iostream>
#include<fstream>
using namespace std;

int wordCount(ifstream dictionary);
string selectWord();


int main()
{
	
	
	string selectedWord = selectWord();
        cout << selectedWord << endl;
	return 0;
	
}

int wordCount(ifstream & dictionary)
{
	ifstream fileIn (dictionary);
	int counter=0;
	char temporal;
	while (fileIn.get(temporal))
	{
		if (temporal == '\n')
		{
			counter++;				
		}			
	}
	fileIn.close();
	return counter;
}


string selectWord()
{
	string myFile("rawDictionary2");
	int totalWords= wordCount(myFile);
	srand((unsigned) time(0));
	unsigned int whichNumber= (rand()%totalWords)+1;
	string word;
	for (int i= 0; i <whichNumber; i++)
	{
		myFile >> word;
	}

myFile.close();
	
	return word;
}
string myFile("rawDictionary2");
int totalWords= wordCount(myFile);

you are sending in a string, why do you want it to accept an ifstream?

string myFile("rawDictionary2");
int totalWords= wordCount(myFile);

you are sending in a string, why do you want it to accept an ifstream?

mrboolf, rawDictionary2 is meant to be a file.

Then it should be

ifstream myFile("rawDictionary2");

So it is actually a file object rather than a string object!

Chris

if u cannot pass by reference,
why do you need to pass an ifstream object.Just declare an instance of ifstream again and initialize that with the same file.

ifstream foo;
foo.open ("whatever be the filename.txt", ifstream::in);
and use it

other option : declare the ifstream object globally.

Hi, thanks for the input.

Freaky_Chris and mrboolf, yeah, sorry it was my mistake when I wrote the thread. For some reason I didn't just copy and paste. Line 38 should be
ifstream myFile("rawDictionary2");
as Freaky_Chris pointed out.

rohitfeb14:

if u cannot pass by reference,
why do you need to pass an ifstream object.Just declare an instance of ifstream again and initialize that with the same file.

ifstream foo;
foo.open ("whatever be the filename.txt", ifstream::in);
and use it

The problem with this approach is that I plan to use the function with more than one file.

Hi all,

thanks for the input. I finally put that to work and found the errors. Just for reference to those interested below is the working code.

Having to close the file and reopen (lines 36 and 37) looks to me pretty poor though and I would appreciate if anybody knows how to make it more elegant.

Thanks

#include<iostream>
#include<fstream>
using namespace std;

int wordCount(ifstream & dictionary);
string selectWord();


int main()
{
	string selectedWord = selectWord();
	cout << selectedWord << endl;
	return 0;
	
}

int wordCount(ifstream & dictionary)
{
	int counter=0;
	char temporal;
	while (dictionary.get(temporal))
	{
		if (temporal == '\n')
		{
			counter++;				
		}			
	}
	return counter;
}


string selectWord()
{
	ifstream myFile("rawDictionary2");
	int totalWords= wordCount(myFile);
	myFile.close();
	myFile.open("rawDictionary2");
	srand((unsigned) time(0));
	unsigned int whichNumber= (rand()%totalWords)+1;
	string word;
	for (int i= 0; i < whichNumber; i++)
	{
		myFile >> word;
	}

	myFile.close();
	
	return word;
}

You could call,

myFile.seekg(0, ios::beg);

P.S. If your problem is solved please mark the thread as solved

Thanks,
Chris

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.