problem in program that finds words that contain a certain letter from a list.

Hi Everyone,
I wrote a program for Linux which goes through a file called List.txt, finds words with the letters M and T in them and then pastes these words in another file called MandTwords.txt. The source code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream iFile;
	ofstream oFile;
	iFile.open("/home/computer/Desktop/List.txt");
	oFile.open("/home/computer/Desktop/MandTwords.txt");
	size_t letterT;
	size_t letterM;
	char item_list[50];
	char list_item[10];
	iFile >> item_list;
	while(iFile.good())
	{
		cin.getline(list_item,10,',');
		letterT=list_item.find('t'); //ERROR!
		letterM=list_item.find('m'); //ERROR
		if((letterT) && (letterM))
		{
			oFile << list_item <<endl;
		}
		else continue;
	}
	oFile.close();
	cout<<"Task completed"<<endl;
	cin.get();
	return 0;
}


I get this error for line 19 and 20

list.cpp: In function ‘int main()’:
list.cpp:19: error: request for member ‘find’ in ‘list_item’, which is of non-class type ‘char [10]’
list.cpp:20: error: request for member ‘find’ in ‘list_item’, which is of non-class type ‘char [10]’


Could someone please help me out with this?
Thanks alot!
You probably meant to make item_list and list_item strings. Even then it must be
if (letterT!=string::npos && letterM!=string::npos)

else continue; is superfluous, by the way.

Also, you shouldn't declare variables before you actually need them (this means letterT and letterM).
Oh Yeah! That was wrong - I just realized it. Thanks.
I made that correction - but the same error persists.
Did you define list_item and item_list as strings as Athar suggested in addition to comparing with string::npos?
No, I didn't convert them to strings, sorry :$
I've modified the code, and it looks like this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream iFile;
	ofstream oFile;
	iFile.open("/home/waqqas/Desktop/List.txt");
	oFile.open("/home/waqqas/Desktop/MandTwords.txt");
	size_t letterT;
	size_t letterM;
	string item_list;//list of items
	string list_item;//single item in list
	iFile >> item_list;
	while(iFile.good())
	{
		getline(item_list,list_item, ',');
		letterT=list_item.find('t');
		letterM=list_item.find('m');
		if((letterT != string::npos) && (letterM != string::npos))
		{
			oFile << list_item <<" ";
		}
	}
	oFile.close();
	cout<<"Task completed"<<endl;
	cin.get();
	return 0;
}		


I now have this error:

list.cpp: In function ‘int main()’:
list.cpp:19: error: cannot convert ‘std::string’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)

Well, I did not realize you had a getline() method in there. Just assign the array in getline() to a string and then use that string's find method. I have provided the code below for your reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream iFile;
    ofstream oFile;
    iFile.open("/home/computer/Desktop/List.txt");
    oFile.open("/home/computer/Desktop/MandTwords.txt");
    size_t letterT;
    size_t letterM;
    string sli;
    char item_list[50];
    char list_item[10];
    iFile >> item_list;
    while(iFile.good())
    {
        cin.getline(list_item,10,',');
        sli = list_item;
        letterT=sli.find('t'); //ERROR!
        letterM=sli.find('m'); //ERROR
        if((letterT != string::npos) && (letterM != string::npos))
        {
            oFile << list_item <<endl;
        }
        else continue;
    }
    oFile.close();
    cout<<"Task completed"<<endl;
    cin.get();
    return 0;
}
1
2
3
4
5
6
7
ifstream iFile;
string item_list;

//If you want to read until you find a '\n'
getline(iFile, item_list);
//if you want to read per word
iFile >> item_list;
Last edited on
Firstly, just want to thank everyone for their help.
The program does compile now - but there's a runtime error. It does not proceed beyong the cin.getline() part. It just halts there. Pressing enter repeatedly forces the loop to proceed and then it just goes on till infinity.
The code is as written by naivnomore above.

Thanks again.
why are you using cin.getline()? Isn't suppose to read from a file?
while(iFile.good()) //you never change the state of iFile
ne555 is absolutely correct. In your code, you are reading the lines from cin which refers to the console but your condition in the while loop checks if your iFile is good. That is why it loops for over. Please change your getline code so that it gets invoked on the iFile object instead of the cin object.
Thanks guys - your comments made a lot of sense. It seems silly to direct getline to cin instead of the string. I fixed that and now the code does actually go right to the end.
Here's what it looks like now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream iFile;
	ofstream oFile;
	iFile.open("/home/waqqas/Desktop/List.txt");
	oFile.open("/home/waqqas/Desktop/MandTwords.txt");
	size_t letterT;
	size_t letterM;
	string item_list;//list of items
	cout<<"Task Initiated:";
	while(getline(iFile, item_list,','))
	{
		letterT=item_list.find('t');
		letterM=item_list.find('m');
		if((letterT != string::npos) && (letterM != string::npos))
		{
			oFile << item_list <<", "; //ERROR LINE 22
			cout <<".";
		}
		
	}
	cout<<"Task completed"<<endl;
	cin.get();
	return 0;
}


there is still a problem with Line 22 though - it doesn't output the words to the text file. When i replace line 22 with:

cout << item_list <<", ";

the words with M and T appear on the screen as expected. But they don't appear on my output file.


Is the file created empty? or is not created at all?
Try this oFile.close(); or oFile << item_list << ", " << flush;
Yup, they both worked and now the program runs perfectly.
Thanks alot, both of you!!!!!!!!!!!!!!!!!!!!!!
You really saved the day.

Topic archived. No new replies allowed.