For loop with fstream help!

I have a for loop that compares the input and a specific line on a text file if they are the same. This is for the log in of my program. But there is something wrong with my for loop. Look

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
	ifstream newfile;
	ifstream myfile;
	ifstream myfile1;
	
	newfile.open("counter.txt");
	if(newfile.is_open()){
		newfile >> ctr;
		newfile.close();
	}


		for(int x=1; x<ctr; x++){
		
			strcpy(concat,"account");
			strcat(concat,"[");
			itoa (x,sub,10);
			strcat(concat,sub);
			strcat(concat,"].txt");
	
			myfile.open(concat);
			
			if(myfile.is_open()){
				
				
				myfile >> usernameConfig;
				cout<<usernameConfig;
				myfile >> passwordConfig;
				cout<<passwordConfig;
				
				if(strcmp(username,usernameConfig)==0 && strcmp(password, passwordConfig)==0){
					cout<<"success";
					x=0;
					break;
				}
				else{
					cout<<"fail";
					
					x=0;
				}
				
				myfile.close();
			}
		}


Like if I input a username and password that is from a text file with index 3, random codes appear forever. Can someone help. Thanks.
Last edited on
Suppose it can't find the username and password in account[1].txt. The test at line 30 fails and and it resets x to zero. The next time through the loop account[0].txt probably doesn't exist. So it goes through the loop again and checks account[1].txt again. The process repeats forever.

Try removing line 38.
Topic archived. No new replies allowed.