Filing in an ATM

This is the code to access the account info from a text file. The problem is that if we somehow input the wrong ID,(which is not in the text file) it somehow lets the acc number be that wrong input and the other variables are filled with garbage value. It messes up the whole ATM program. Can someone correct the error? I want the function to return false if the account is not present in the file.

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
  bool accessaccount(int input){
			
			string temp;
			int temp2;
		ifstream notmyfile ("Accounts.txt");
		
		
		do{
			
		notmyfile >> temp2;
		
		
		if (temp2 == input){
			Accno = temp2;
		
			notmyfile >> PIN;
			cout << "Enter PIN: "; cin >> temp2;
			if(PIN == temp2){
				notmyfile 	>> Amount	>> Fname	>> Lname;
			
		}	
			else return false;
			
		}
		else{
			for(int a=0; a<4; a++)
			notmyfile >> temp;
			
		}
		
	}while(!notmyfile.eof());
}	
At line 20, you want return true; because you found a match.

You want to remove line 22. That will cause you to exit with false on the first non-match.

You want your return false; after line 31 when you have read the entire file and not found a match.

Thanks alot sir, it helped.
Topic archived. No new replies allowed.