Need help

I am making a username and password database, my problem is that the program only see's the first user in the array and not any other users. I am sure this is a simple fix, or maybe I am grabbing the information wrong. Thank you in advance.

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


int main(int argc, char** argv) {
    string data[10][2];
    ifstream fin;
    string name="";
    string pass="";
    int i=0;
    fin.open("users.txt");
    if(fin.is_open()) {
        while(!fin.eof()){
            fin>>name>>pass;
            //Outputs the name and password from the file
         //   cout<<name<<" "<< pass<<endl;
            data[i][0]=name;
            data[i][1]=pass;
            i++;
        }
    }
    //Display the user's and passwords in the array
//    for(int i=0;i<10;i++){
//        for(int j=0;j<10;j++){
//            if(data[i][j] !=" "){
//                cout<<data[i][j]<<" ";
//            }
//        }
//    }
//    cout<<endl;
    string user ="";
    string password ="";
    cout<<"Input your username\n";
    cin>>user;
    for(int i=0;i<10;i++){
            if(data[i][0] == user){
                cout<<"Input your password\n";
                cin>>password;

                if(data[i][1] == password){
                    cout << "Welcome back " << data[i][0] << endl;
                    cout << "Password: " << data[i][1] << endl;
                    break;
                }else{
                    cout<<"Bad PassWord\n";
                    break;
                }
            }else{
                cout<<"Invalid Username\n";
                break;
            }
    }
    return 0;
}

In-File
1
2
3
Hector 1234
Mango 0000
mango 1234
> the program only see's the first user in the array and not any other users.
Do a flow diagram.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    for(int i=0;i<10;i++){
            if(/**/){
                //...
                if(/**/){
                    //...
                    break;
                }else{
                    //...
                    break;
                }
            }else{
                //...
                break;
            }
    }
Thank you for your comment, but I don't see how this helps me. I already have that, just I'm having problems with comparing what's inside my 2D array to what the user inputs.
Line 50, will break operation on first check, you might want to for loop continue unless all names have been checked.

see comments..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	for (int i = 0; i<10; i++) {
                // putting this inside for loop to redisplay the question
		cout << "Input your username\n";
		cin >> user;
		if (data[i][0] == user) {

                /// the rest of the code ...

                // if username is wrong check next one, also show checking info.
		else {
                        cout<<"current check is: " << i + 1 << "\n";
                        if(i == 9) cout<<"Invalid Username\n"; // if none checks passed, show fail.
			continue; // continue until i < 10, if i == 9 for loop will break.
		}
	}


there are other things worth considering such what will program do if file fails to open?
consider making checks for that too.
Last edited on
Thank you so much, I didn't realize that it was breaking after the first one. I will be sure to rewrite my code to have a bool variable.
Last edited on
Topic archived. No new replies allowed.