comparring arrays

ok i have another noob question i have a program that asks for an account number

1
2
3
4
5
6
7
8
cout << "Enter one Account number(Between 1000 and 9999) at a time: " << endl;				// Prompt for an account number
			cin >> accountNum[count];
		if (accountNum[count] < 1000 || accountNum[count] > 9999)
		{
			cout << "Error! Please try again." << endl;
			cout << "Enter one Account number(Between 1000 and 9999): " << endl;
			cin >> accountNum[count];
		}


what im trying to do is check whether an account number has already been entered
what code i have for the check is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool doAgain = false;
		do 
		{
			for(int h=0;h<=5;h++) //for loop to access the first data
			{
				for(int i=0;i<ACCOUNT-1;i++) //for loop to check the first data with the rest of the data in the array
				{
					if(accountNum[h] == accountNum[i])
					{
						cout << "Error! account num already taken" << endl;
						doAgain = true;
						break;
					}
				}
			}
		}while(doAgain);


and i dont think the lodgic is right its not working and i dont know if im thinking it through the right way any ideas???
It's not clear what some of the data types are in your program. What data type is accountNum, what is the value of count, and ACCOUNT?

Assuming that accountNum is an integer or even a char this code:
if (accountNum[count] < 1000 || accountNum[count] > 9999)

will compare the integer/character at location count with 1000 and 9999 respectively.

I would suggest that you store your account number in a string object, (see: http://cplusplus.com/reference/string/string/), you can then do a direct equality comparison == or use the string::compare(...) function to see if it is equal to or greater than or less than.

If you must use arrays use a character array (char accountNum[128], but beware you can not compare character arrays using the equality operator ==, you must use the strcmp(...) function (see: http://cplusplus.com/reference/clibrary/cstring/strcmp/ ).

Hope this helps!
Sorry im still in class so here are the values
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
	const int ACCOUNT = 5;
	double totalBal;
	int count = 0;
	int accountNum[5];

for(count=0;count<ACCOUNT;++count)
	{
		cout << "Enter the account term from 1 to 10 years" << endl;							// Account terms prompt
		cin >> accountNumTerm[count];
		while (accountNumTerm[count] < 1 || accountNumTerm[count] > 10)
		{
			cout << "Error! Please try again" << endl;
			cout << "Enter the first account term from 1 to 10 years" << endl;
			cin >> accountNumTerm[count];
		}
			cout << "Enter one Account number(Between 1000 and 9999) at a time: " << endl;				// Prompt for an account number
			cin >> accountNum[count];
		while (accountNum[count] < 1000 || accountNum[count] > 9999)
		{
			cout << "Error! Please try again." << endl;
			cout << "Enter one Account number(Between 1000 and 9999): " << endl;
			cin >> accountNum[count];
		}
		bool doAgain = false;
		do 
		{
			for(int h=0;h<=5;h++) //for loop to access the first data
			{
				for(int i=0;i<ACCOUNT-1;i++) //for loop to check the first data with the rest of the data in the array
				{
					if(accountNum[h] == accountNum[i])
					{
						cout << "Error! account num already taken" << endl;
						doAgain = true;
						break;
					}
				}
			}
		}while(doAgain);

so will it still work using a char array?
apologies, I think I misunderstood you code.

Post up your full solution, let me have a look at it.
Sometimes when you have given someone bad advice you just gotta hold your hands up and admit it, ignore what I wrote above, I totally misunderstood what you were asking. :)

O.k. I've looked at your code and for the most part it seems good, until:

1
2
3
4
5
6
7
8
9
10
11
for(int h=0;h<=5;h++) //for loop to access the first data	
{
	for(int i=0;i<ACCOUNT-1;i++) //for loop to check the first data with the rest of the data in the array
	{
		if(accountNum[h] == accountNum[i])			{
			cout << "Error! account num already taken" << endl;
			doAgain = true;
			break;
		}
	}
}

particularly if(accountNum[h] == accountNum[i]).

I think that you are trying to search through a pre-existing array of account numbers, and you are trying to see if the account number entered already exists.

In which case this is wrong, because you are searching through the array where you have just stored the account number you are checking for so it WILL LEAD TO: "Error! account num already taken" being displayed (when h == 0 && i == 0) because your checking the if the same thing is the same as itself (if you get me).

E.g:
where h == 0 && i == 0 your code becomes:

if( accountNum[0] == accountNum[0] )

The solution is changing: cin >> accountNumTerm[count], store the given account number in a separate integer, check this ONE VALUE against the values stored in the accountNum array.

1
2
3
4
5
6
7
8
9
10
11
int nGivenAccoutNumber = 0;

//get input from user and validate

for(int i = 0; i < 5; i++)
{
    if(nGivenAccountNumber == accountNum[i])
    {
         //account number already exists so perform the appropriate behavior
    }
}


Another thing, the way your do while loop is structured will cause it to loop infinitely if the user enters an account number that already exists in the accountNum array.

Your do while loop should (if I have interpreted your code correctly) should really encompass all the user input (it should start at line 8), so that the user can respond and correct any error they make.

I think that covers it. Apologies for the earlier confusion! If you need any more help feel free to ask, and I'll try not to make a pig ears of it next time :D

Hope this help!
Last edited on
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/****************************
 * Adam Koen                *
 * Chapter 5  case 2         *
 * 3/31/2011                *
 ****************************/
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    /*********************
     * declare structures*
     *********************/
	const int ACCOUNT = 5;
	double totalBal;
	int nGivenAccountNumber = 0;
	int count = 0;
	int index = 0;
	int searchterm;
	int accountNum[5];
	int accountNumBal[5];
	int accountNumTerm[5];
	int accountInt[5];
	int found = 0;
	int x;
	
	/*****************************
     *     ACCOUNT INFORMATION   *
     *    Populate structures    *
     *****************************/
	cout << "WELCOME TO ADAM'S ACCOUNT SOFTWARE" << endl;
	cout << endl;
	cout << endl;
	cout << endl;
	for(int j=0;j<ACCOUNT;++j)
	{
		cout << "Enter the account term from 1 to 10 years" << endl;							// Account terms prompt
		cin >> accountNumTerm[count];
		while (accountNumTerm[count] < 1 || accountNumTerm[count] > 10)
		{
			cout << "Error! Please try again" << endl;
			cout << "Enter the first account term from 1 to 10 years" << endl;
			cin >> accountNumTerm[count];
		}
			cout << "Enter one Account number(Between 1000 and 9999) at a time: " << endl;				// Prompt for an account number
			cin >> accountNum[count];
		while (accountNum[count] < 1000 || accountNum[count] > 9999)
		{
			cout << "Error! Please try again." << endl;
			cout << "Enter one Account number(Between 1000 and 9999): " << endl;
			cin >> accountNum[count];
		}
                // right here is where im putting in the code from earlier
		for(int i = 0; i < 5; i++)
		{
			if(nGivenAccountNumber == accountNum[i])
			{
         //account number already exists so perform the appropriate behavior
				cout << "Error! account num already taken" << endl;
			 }
			accountNum[count] = nGivenAccountNumber;
		}
		//bool doAgain = false;
		//do 
		//{
		//	for(int h=0;h<=5;h++) //for loop to access the first data
		//	{
		//		for(int i=0;i<ACCOUNT-1;i++) //for loop to check the first data with the rest of the data in the array
		//		{
		//			if(accountNum[h] == accountNum[i])
		//			{
		//				cout << "Error! account num already taken" << endl;
		//				doAgain = true;
		//				break;
		//			}
		//		}
		//	}
		//}while(doAgain);



		cout << "Enter the Account Balance(Can not be less than 0 or more than 100000): " << endl;			// Prompt for an account balance
		cin >> accountNumBal[count];

		while (accountNumBal[count] <= 0 || accountNumBal[count] > 100000)
		{
			cout << "Error! Please try again." << endl;
			cout << "Enter the first Account Balance(Can not be less than 0 or more than 100000): " << endl;
			cin >> accountNumBal[count];
		}
		cout << "Enter the account interest rate 0.1 to 0.15: " << endl;									// Prompt for an intrestrate
		cin >> accountInt[count];
		while (accountInt[count] <= 0 || accountInt[count] > 15)
		{
			cout << "Error! Please try again." << endl;
			cout << "Enter the account interest rate 1 to 15: " << endl;
			cin >> accountInt[count];
		}
		totalBal = 0;
		if (accountNumBal[count] > 0)
		{
			totalBal += accountNumBal[count];
		}
		// = accountNumBal[count] + accountNumBal[count] + accountNumBal[count] + accountNumBal[count] + accountNumBal[count];
		cout << " Count " << count + 1 << " Accout Number " << accountNum[count] << " Balance $" << accountNumBal[count] << " Term " << accountNumTerm[count] << "yrs " << " Interest Rate " << accountInt[count] << endl;
		cout << "\nNEXT ACCOUNT" << endl;
		count++;

	}
	/*****************************************
	 *   Output of all accounts entered      *
	 *****************************************/
	for (int a=0;a<ACCOUNT;++a)
	{
    cout << " Accout Number " << accountNum[a] << " Balance $" << accountNumBal[a] << " Term " << accountNumTerm[a] << "yrs " << " Interest Rate " << accountInt[a] << endl;
	}
	/*cout << " Accout Number " << accountNum[1] << " Balance $" << accountNumBal[1] << " Term " << accountNumTerm[1] << "yrs " << " Interest Rate " << accountInt[1] << endl;
	cout << " Accout Number " << accountNum[2] << " Balance $" << accountNumBal[2] << " Term " << accountNumTerm[2] << "yrs " << " Interest Rate " << accountInt[2] << endl;
	cout << " Accout Number " << accountNum[3] << " Balance $" << accountNumBal[3] << " Term " << accountNumTerm[3] << "yrs " << " Interest Rate " << accountInt[3] << endl;
	cout << " Accout Number " << accountNum[4] << " Balance $" << accountNumBal[4] << " Term " << accountNumTerm[4] << "yrs " << " Interest Rate " << accountInt[4] << endl;*/
	cout << "Total between all accounts " << totalBal << endl;
	cout << "Average account balance " << totalBal / count << endl;
	cout << endl;
	cout << endl;

	/*****************************
	 *  Array Search for account *
	 *****************************/
	cout << "\nSEARCH FOR AN ACCOUNT" << endl;
	cout << "Please enter an account number to search for(Enter 999 to quit) " << endl;
	cin >> searchterm;
	while (searchterm != 999)
	{
		//x = 0;
		for (x = 0; x < count; ++x)
		{
			if ( searchterm == accountNum[x])
			{
				cout << "account found! " << accountNum[x] << " has a balance of $"
					<< accountNumBal[x] << " term " << accountNumTerm[x] << "yrs "
					<< " With an interest rate of " << accountInt[x] << endl;
				found = 1;
			}
			if (found == 0)
			{
				cout << "Sorry account not found!" << endl;	
			}

		}
		cout << "Please enter an account number to search for(Enter 999 to quit) " << endl;
		cin >> searchterm;
	}
	cout << "That you for using Adam's banking software have a nice day!" << endl;
	cout << endl;
	cout << endl;
    system("pause");
    return 0;
}
Last edited on
Hi, I've not really had time to check through the code, but having skimmed through it this topic maybe useful: http://www.cplusplus.com/forum/beginner/1988/
Topic archived. No new replies allowed.