problem in the internal loop

I have a piece of code like below. when I run code, "cout" shows on screen that the internal loop just one time run (for linenoq = 0). I mean when linenoq is equal to 0, then linenoq1 counts 0 to 3. But for other linenoq, i.e. from 1 to 3, the internal loop does not work.

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
  using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

int main() {
	int CN=0, N1=0, N2=0, N3=0, N4=0, CN_1=0, N1_1=0, N2_1=0, N3_1=0, N4_1=0;
	string line, line1;
	
	ifstream myfile ("NB.txt");
	ifstream myfile1 ("NB1.txt");
	if (myfile.is_open())
	{
		ofstream secondoutput_11;
		secondoutput_11.open ("11.txt");
			  
		for (int linenoq = 0; getline (myfile,line) && linenoq < 4; linenoq++){	
			myfile>>CN>>N1>>N2>>N3>>N4;
			cout<<"linenoq : "<<linenoq<<endl;
			
			myfile1.seekg(0, myfile1.beg);
			
			for (int linenoq1 = 0; getline (myfile1,line1) && linenoq1 < 4; linenoq1++){	
				myfile1>>CN_1>>N1_1>>N2_1>>N3_1>>N4_1;
				
				if (CN_1>CN){
					cout<<"linenoq1 : "<<linenoq1<<endl;
					
					if (N1==N1_1||N1==N2_1||N1==N3_1||N1==N4_1){
						secondoutput_11<<CN<<" "<<CN_1<<" "<<N1<<endl;
					}
					if (N2==N1_1||N2==N2_1||N2==N3_1||N2==N4_1){
						secondoutput_11<<CN<<" "<<CN_1<<" "<<N2<<endl;
					}
					if (N3==N1_1||N3==N2_1||N3==N3_1||N3==N4_1){
						secondoutput_11<<CN<<" "<<CN_1<<" "<<N3<<endl;
					}
					if (N4==N1_1||N4==N2_1||N4==N3_1||N4==N4_1){
						secondoutput_11<<CN<<" "<<CN_1<<" "<<N4<<endl;
					}
				}
			}
		}
		secondoutput_11.close();
	}
	else cout << "Unable to open file"<<endl;
}


what is shown on screen:
linenoq : 0
linenoq1 : 1
linenoq1 : 2
linenoq1 : 3
linenoq : 1
linenoq : 2
linenoq : 3

how should code be modified so as to I have something like below:
linenoq : 0
linenoq1 : 1
linenoq1 : 2
linenoq1 : 3
linenoq : 1
linenoq1 : 2
linenoq1 : 3
linenoq : 2
linenoq1 : 3
linenoq : 3
Well if your inner loop manages to read end of file, then you get stuck.

> myfile1.seekg(0, myfile1.beg);
http://www.cplusplus.com/reference/ios/ios/clear/

Note the change of semantics between C++98 and C++11.
http://www.cplusplus.com/reference/istream/istream/seekg/

Dear salem c, so many thanks for help. Indeed, I did not pay attention to "http://www.cplusplus.com/reference/ios/ios/clear/". you solved one of the my big problem.
Topic archived. No new replies allowed.