Help with array

Hi everybody.

I'm working on a ATM Machine program for my class and I'm having a hard time creating an array.

The array is for the user id, which should be from 0 to 9. So, the array should be something like:

userID[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

If the ID is correct then the user should be allowed in the system.

I know how to create the array but I don't know how to get an ID typed by the user and see if it is in the array. How do I do that? Thanks.
Have you learned about cin ?
http://cplusplus.com/reference/iostream/cin/?kw=cin

1
2
3
4
 
  int id;
  cin >> id;
  // Search array for match on id  

Yes I have. My question is how to check, for example, if the value in that id variable is in the array.
Why not just make it a long?

If you need a bigger number, this is the max limit of:
unsigned long long: 0 to 18446744073709551615

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
	long userID = 1234567890;
	long enteredUserID;

	std::cout << "Your ATM machine.\n\n";

	std::cout << "Please enter your user ID: ";
	std::cin >> enteredUserID;

	if( enteredUserID == userID )
		std::cout << "Correct!\n";
	else
		std::cout << "Incorrect!\n";

	return 0;
}
Last edited on
That's not quite what I'm looking for. I have an array with the numbers 0 thru 10. I need to get an input from the user and see if this input is in the array or not. I need an array because there will be 10 different accounts.
Last edited on
You mean something like this:
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
#include <iostream>

//int main()
void peoplesPrograms()
{
	long userID[ 10 ] = { 321654,
	                      654987,
	                      321489,
	                      321449,
	                      123588,
	                      159753,
	                      467913,
	                      110235,
	                      445896,
	                      777952};
	long enteredUserID;

	std::cout << "Your ATM machine.\n\n";

	std::cout << "Please enter your user ID: ";
	std::cin >> enteredUserID;

	for( int i = 0; i < 10; ++i )
	{
		if( enteredUserID == userID[ i ] )
			std::cout << "Correct!\n";
		else
			std::cout << "Incorrect!\n";
	}

	//return 0;
}


Obviously needs some fixing, in the if-statement, but my food is done! ahaha (:
Ok, I made a few changes:

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
#include <iostream>

using namespace std;

void peoplesPrograms();

int main()
{
	long userID[ 10 ] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	long enteredUserID;

	cout << "Your ATM machine.\n\n";

	cout << "Please enter your user ID: ";
	cin >> enteredUserID;

	for( int i = 0; i < 10; ++i )
	{
		if( enteredUserID == userID[ i ] )
			cout << "Correct!\n";
		else
			cout << "Incorrect!\n";
	}

	return 0;
};


And my output is:

1
2
3
4
5
6
7
8
9
10
11
12
13
Your ATM machine.
Please enter your user ID: 1
Incorrect!
Correct!
Incorrect!
Incorrect!
Incorrect!
Incorrect!
Incorrect!
Incorrect!
Incorrect!
Incorrect!
Press any key to continue . . .


The array keeps checking regardless if the value is in the array or not. How do I make it print "Correct" only once if the entered value is in the array or "Incorrect" only once if the value is not correct?
Last edited on
But you're not going to cout stuff! Right? You just want to know if the user was found.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	bool userIdentified = false;

	for( int i = 0; i < 10; ++i )
	{
		if( enteredUserID == userID[ i ] )
		{
			userIdentified = true;
			break; // Break the loop when a user is found.
		}

	}

	if( userIdentified )
		std::cout << "You were found in the system\n.";
Easiest way is to put the check in a functin:
1
2
3
4
5
6
7
bool check_id (const long & accts[], long num)
{  for( int i = 0; i < 10; ++i )
   {   if  (num == accts[i])
           return true;  // found match
    } 
    return false;  // no match
}


Then in main:
1
2
3
4
    if (check_id (userID, enteredUserID)) 
        cout << "Correct!\n";
    else
       cout << "Incorrect!\n";

I tried AbstractionAnon's code and it didn't work. Visual Studio 2010 accused that arrays of reference are not allowed. Lynx876's solution worked like a charm! Thanks a lot guys! I just need to improve my algorithm making.
Sorry. The & shouldn't have been there.
 
bool check_id (const long accts[], long num)


Great. It works. Thanks a lot guys!
It would be cleaner to use AbstractionAnon's code.

If for any reason you need to read the user ID's again, you'd have to re-write the code segment where you need to check it.

Just simply use the fix, in the above post. Arrays are are passed, by default, by reference. That's why you received the error. (:
It works like a charm! Thanks. I'll need to do a loop later to go back to the main menu, so, I used AbstractionAnon's solution. Here is what it looks like. I've added a few more lines of code for the ATM operations. It really doesn't do anything yet, I will still add functions for check balance, withdraw, and deposit. Thanks guys.

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
#include <iostream>

using namespace std;

bool check_id (const int accts[], long num)
	{  
		for( int i = 0; i < 10; ++i )
	    {   
		   if  (num == accts[i])
           return true;  // found match
		} 
    return false;  // no match
	}

int main()
{
	
	int userID[ 10 ] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	int enteredUserID;
	int choice;
	
	cout << "Enter an id: ";
	cin >> enteredUserID;

	if (check_id (userID, enteredUserID)) 
	{
                system("CLS");
		cout << "Welcome to Anderbank, N.A." << endl;
		cout << "Main menu" << endl;
		cout << "1: check balance" << endl;
		cout << "2: withdraw" << endl;
		cout << "3: deposit" << endl;
		cout << "4: exit" << endl;
		cout << "Enter a choice: ";
		cin >> choice;

		switch (choice)
		{
			case 1: cout << "The balance is: " << endl; break;
			case 2: cout << "Enter amount to withdraw: " << endl; break;
			case 3: cout << "Enter amount to deposit: " << endl; break;
			case 4: break;
			default: cout << "Invalid Option"; break;
		}
	}
    else
       cout << "Incorrect!\n";

	

		return 0;
};
Last edited on
Topic archived. No new replies allowed.