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.
#include <iostream>
usingnamespace 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?
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.";
bool check_id (constlong & accts[], long num)
{ for( int i = 0; i < 10; ++i )
{ if (num == accts[i])
returntrue; // found match
}
returnfalse; // no match
}
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.
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.