Help with loop searching file

I am trying to write a program that will aid me in some translation work. It takes a string, tokenizes it, and translates each word individually. My problem is that it is only testing the first word in my vector and returning "not found" for the remaining words.

It should do this:
Enter a string: day night
day: giorno
night: notte

But it does this:
Enter a string: day night
day: giorno
night: not found

The second word is never found regardless of what happens with the first word.

The class I created isn't necessary, but I did that because I learned that yesterday in class and wanted to practice a bit.

Here's my code, thanks for the help.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <algorithm>

using namespace std;
class vout{
public:
	vector<string>st;
	int wcount;
	void print(){
		int i(0);
		for (;i<wcount;i++)
			cout << st[i] << "\n";
	}

};

int main()
{
	string str,buf;
	vout stack;
	int bsize,flag=0;
	int i;

	cout << "Enter a string: ";
	getline(cin,str);
	transform(str.begin(),str.end(),str.begin(),tolower);

	stringstream ss(str);

	while (ss >> buf){
		stack.st.push_back(buf);
	}
	stack.wcount=stack.st.size();

	ifstream f("c:/temp/Italian.txt");
	if (!f.is_open())
		cout << "file not open";

	for (i=0;i<stack.wcount;i++){ 
		while (getline(f,str)){
			stringstream ss(str);
			ss >> buf;
			if (!buf.compare(stack.st[i])){
				bsize=buf.size();
				cout << stack.st[i] << ":" << str.substr(bsize) << "\n";
				flag++;
			}	
		}
			if (!flag)
				cout << stack.st[i] << " :\t" << "not found\n";
			flag=0;		
	}
	return 0;
}

My guess is at the end of this loop:

1
2
3
while (getline(f,str)){
...	
}


f is at the end of file. You should close and reopen the file before entering the loop again.
thank you, that worked
Topic archived. No new replies allowed.