connot fix errors

Id like it to compare the words... But i only get the word cat once and i need to get it twice...

//The dic.txt
cat
rock
ape
lame
cat


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
		ifstream in("dic.txt");
		char append[5] = "cat";
		string appendnew(append);
		string line;
		getline (in, line);
		while(getline(in, line))
		{
		if(line.compare(appendnew)==0)
		{					
			cout << "Found word: " << line << endl;
		}	
		}
		in.close();	
		system("PAUSE");
		return 0;
}
Last edited on
Actually, I think you do get the word twice. It just happens to be discarding the first occurrence read on line 13 and immediately overwritten on line 14, before the testing begins.

EDIT: That is assuming that your data file starts with cat on line 1, like this:
cat
rock
ape
lame
cat


Last edited on
Topic archived. No new replies allowed.