Transaction functions uses same username.

In the program, I have entered to 2 usernames. In the program, I entered with the first username with index 0 and the second username with index 1. When I tried the transaction function using the first username, it works. But the second one doesn't because the transaction functions thinks only the first username exists. What did I do wrong here?

Login function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void login(InventoryRecord list[], int size) {

	int i = 0;
	string userMatch;
	string passMatch;

		cout << "\nEnter username: ";
		cin >> userMatch;
		for (i = 0; i < size; i++) {
			if (list[i].username == userMatch) {
				cout << "\nEnter password: ";
				cin >> passMatch;
				if (list[i].password == passMatch) {
					transaction(list,size);
				}
			}
		}

	}
}


Transaction function:
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
void transaction(InventoryRecord list[], int size) {
	system("cls");
	bool not_done = true;
	int i = 0;
	list[size];
	char choice;

	do {
		system("cls");
		cout << "Select one of the following transactions:" << endl;
		cout << "\t****************************" << endl;
		cout << "\t    List of Choices         " << endl;
		cout << "\t****************************" << endl;
		cout << "\t     W -- Withdrawal" << endl;
		cout << "\t     D -- Deposit" << endl;
		cout << "\t     V -- View Account" << endl;
		cout << "\t     L -- Log out" << endl;
		cout << endl << "\tEnter your selection: ";
		cin >> choice;

		switch (choice) {
		case 'W':
		case 'w':
			withdraw(list, size);
			cout << "Current balance :" << list[i].balance << endl;
			system("pause");
			break;
		case 'D':
		case 'd':
			deposit(list, size);
			cout << "Current balance :" << list[i].balance << endl;
			system("pause");
			break;
		case 'V':
		case 'v':
			cout << fixed << setprecision(2);
			cout << "Username       Account No       Balance" << endl;
			cout << "----------------------------------------" << endl;
			cout << left;
			cout << setw(21) << list[i].username << right
			<< setw(4) << list[i].acc_no << setw(10) << list[i].balance << left << endl;
			system("pause");
			break;
		case 'L':
		case 'l':
			not_done = false;
			system("cls");
			break;
		}
	} while (not_done);
}
Last edited on
the transaction functions thinks only the first username exists

You pass it only the first username, what should it ‘think’?
cout << "\nEnter username: ";
cin >> userMatch;

should go inside the for-loop as well.

In general, do avoid declaring all your variables at the beginning of functions. Define them where you really need them.

Topic archived. No new replies allowed.