proram error

error .....
Last edited on
Please please please format your code nicely between code tags.

I've fixed it, and commented the changes.

Also I removed the extra using namespace std; from inside the main function, which is unnecessary after the using namespace std; at the top.

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int MAX_ACCOUNT = 20; /* If the three arrays use the same indexing,
		there's no point having more than one size.*/
	int idNo;
	string info;

	int accountNo [MAX_ACCOUNT] = {
		7221379, 6853921, 4215534, 5551298,
		6224649, 6502286, 8448936, 2883903,
		6752376, 3564698, 6373150, 6375372,
		8736368, 7218536, 6681985, 3909086,
		5221192, 5901163, 2427821, 633165
	};

	double balance[MAX_ACCOUNT] = {
		970.12, 268.01, 3821.94, 3193.97,
		444.21, 4253.79, 2916.99, 906.31,
		1291.99, 2001.18, 2963.20, 1152.60,
		1113.05, 3703.46, 2667.88, 3042.25,
		72.04, 2229.66, 3130.92, 3004.99
	};

	string name1 [MAX_ACCOUNT] = {
		"Abula","Allen","Austin","Bailey",
		"Balovich","Bates","Bettencourt","Blackburn",
		"Boucher","Brown","Duncan","Estrada",
		"Fernandez","Gallagher","Gonzales","Ha",
		"Hernandez","Howard","Johnston","Nguyen"
	};

	cout << " Enter Name or Account: " << endl;
	getline (cin , info);

	if(info == "name")
	{
		string perName;
		
		cout << "Enter your name:" << endl;
		
		getline(cin, perName);
		
		cout << perName << endl; //DEBUG
		cout << name1[0] << endl; //DEBUG
		
		bool correct = false;
		int c;
		
		for (c = 0; c < MAX_ACCOUNT; ++c)
		{
			if (name1[c] == perName)
			{
				correct = true;
				break; // See long reason below for why this is needed.
			}
		}
		
		if (correct)
		{
			cout << "Account Name: " << name1[c] << endl ;
			cout << "Account balance: " << balance[c] << endl;
			cout << "Account No: " << accountNo[c] << endl;
			return 0;
		}
		else
		{
			cout << perName << " does not exist." << endl;
		}
	}
	else
	{
		cout << "Enter your account No:" << endl;
		cin >> idNo ;

		int c = 0;
		bool correct = false;

		for ( c = 0; c < MAX_ACCOUNT; c++)
		{
			if (accountNo[c] == idNo)
			{
				correct = true;
				break; /* You were right the first time with this;
					after all when you have the right value of c you want to keep it.
					
					The program crashes because without the break after finding
					the correct value it carries on looping until
					c is MAX_ACCOUNT, which is obviously beyond your array indexes! */
			}
		}

		if (correct)
		{
			cout << "Account Name: " << name1[c] << endl;
			cout << "Account balance: " << balance[c] << endl ;
			cout << "Account No: " << accountNo[c] << endl;
			return 0;
		}
		else
		{
			cout << idNo << " Invalid Aaccount No." << endl;
		}
	}


}
Topic archived. No new replies allowed.