Need a new set of eyes

Hey guys,
Just need a new set of eyes on this code - something is up when the Username or Password is incorrect - the console crashes completely instead of giving error and looping back around. If the name/pass is correct, it works just fine - should be relatively simple, I think I've just been looking at code too much lately and have something out of place. Any tips on how to improve are more than welcome but, even if you have a better idea, I'd still love to know what's causing the issue. Thanks!

P.S.


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Network Login
// User/Password Attempt 2.0

#include <iostream>
#include <string>
#include <fstream>

#define USERS 5 //Max users in system

using namespace std;

struct Character
{
	string Username;
	string Password;
};

Character Users[USERS];
void loadSaves();

int main() 
{
	string user, pass;
	bool success;
	
	loadSaves(); //Will load any previously saved username/password

	cout << "\tNetwork Login\n";

	while(true)
	{
		cout << "\nUsername: ";
		cin >> user; 

		cout << "Password: ";
		cin >> pass; 
		
		for(int i = 0; !success; i++){
			
			if(user == Users[i].Username && pass == Users[i].Password)
			{
				cout << "\nWelcome back, " << Users[i].Username << ". //any other message...";
				success = true;
			}	

		}
		
		if(success) break;
		
		else
		{
			cout << "\nYour login failed.\n";
		}
		
	}
	
	

    return 0;
}


void loadSaves()
{
	
   		ifstream saveFile;
		saveFile.open("fileName.txt");
		string output;
		Character temp;
		
		if (saveFile.is_open())
		{
			while(!saveFile.eof())
			{
				for(int i = 0; i < USERS; i++)
				{
					getline(saveFile,temp.Username);
				
					getline(saveFile,temp.Password);
					
					Users[i] = temp;
				}
				
			}
		}
	
		saveFile.close();
		
}
Last edited on
If there is no match for login and password, then success will never become true, and after several iteration index i will go out of bounds and you will access some random memory.
Perfect - fixed. Thank you!
Topic archived. No new replies allowed.