[C++] Array Comparison

A simple problem as a password comparison using arrays, I've been doing this for 21 hours and this simply doesn't work. I don't know where are my errors. Begging for help & would back anyone who can help me solve my problem in a knife fight! Thanks.

*What I basically need to do in this program is to compare two five-digit passwords to see if they match or not. I also need to be able to reset the password to a new five-digit password, and disallow it to be the same as the old password. Nothing apparently works...

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

const int size = 5;

// test whether the first ‘SIZE’ elements of arrays A and B are equal

bool equal(int A[], int num[], int SIZE) {
	int i;
	if (num[SIZE] == A[SIZE]) {
		cout << "Please re-enter your password." << endl;
		cin >> num[SIZE];
		return false;
	}
	else
		return true;
}

void reset(int A[], int num[], int SIZE) {
	int i;
	int flag;
	cin >> num[SIZE];
	for (i = 0; i < SIZE; i++) {
		cin >> num[i];
	}
	if (A == num) {
		flag = equal(A, num, SIZE);
		cout << "The password you entered matched the current password.\n";
	}
	else
		cout << "New password has been set.\n";
}

bool guessw(int A[], int guess[], int SIZE) {
	int i;
	for (i = 0; i < SIZE; i++) {
		if (A[SIZE] == guess[SIZE]) {
			return true;
		}
		else {
			return false;
		}
	}
	return 0;
}

int main() {
	int choice, guess[size], num[size];
	int A[size] = {9, 1, 6, 5, 1};
	cout << "Enter your choice:" << endl;
	cout << "(1) Guess Password" << endl;
	cout << "(2) Reset Password" << endl;
	cout << "(3) Quit" << endl;
	cin >> choice;
	switch(choice)
	{
	case 1: 
		cout << "Please enter the password of guess." << endl;
		cin >> guess[size];
		guessw(A, guess, size);
		if (guessw(A, guess, size))
			cout << "You got it right!" << endl;
		else
			cout << "You got it wrong!" << endl;
		system("ABORT");
		break;
	case 2:
		cout << "Please enter a new password." << endl;
		cin >> num[size];
		reset(A, num, size);
		break;
	case 3:
		cout << "Quit." << endl;
		break;
	default:
		cout << "Invalid choice; program will be terminated." << endl;
		break;
	}

	return 0;
}
Last edited on
This cin >> guess[size]; almost certainly does not do what you think it does.

Note that guess[5] is off the end of the array. When you create an array with five elements, they are numbered 0,1,2,3, and 4.

Stick this in right after it and see what you get:

cout << guess[0] << " " << guess[1] << " " << guess[2] << " " << guess[3] << " " << guess[4];
Last edited on
Topic archived. No new replies allowed.