Finding a number in an array.

So I need to make a bank account program,and at one point I need to have the user input an ID to check if there is an account with the same number in the arrays. I am completely lost and don't know how to go about it.
I already have the 3 set of ID's along with the 2nd set of balances on a notepad
file using fstream.


int findacct(int acctnum_array[],int num_accts,int account)
{
cout << "Enter account number: " << endl;
cin >> account;
if (account == acctnum_array[num_accts])
{cout << "working";}
else
{cout << "wrong";}

}

-----------------------------------------------------------------------------------------------

The full program

-----------------------------------------------------------------------------------------------

#include<iostream>
#include<fstream>

using namespace std;

int read_accts(int acctnum_array[],double balance_array[],int max_accts);
void print_accts(int acctnum_array[],double balance_array[],int num_accts);
int findacct(int acctnum_array[],int num_accts,int account);

void menu();

int main()

{
const int maxaccts=50;
int accnum[maxaccts],count;
double balance[maxaccts];
//char choice; //Input variable.
//bool incomplete = true; //Sentinel, if false stop program.


menu();
count =read_accts(accnum,balance,maxaccts);
print_accts(accnum,balance,count);

findacct;


/*do { //Do while choice does not equal Q or q.
cout << endl << "Make a selection: " << endl;

switch(choice)

{
case 'Q':
case 'q':
incomplete = false;
cout << "Complete." << endl;
break;

case 'W':
case 'w':

case 'D':
case 'd':

case 'N':
case 'n':

case 'B':
case 'b':

default:
cout << choice << " is not a valid choice." << endl; //If anything else,invalid.
break;

}
*/

return 0;
}

void menu()
{
cout << "Select one of the following: " << endl << endl << "W - Withdrawal" << endl << "D - Deposit" << endl
<< "N - New account" << endl << "B - Balance" << endl << "Q - Quit" << endl << endl;
}

int read_accts(int acctnum_array[],double balance_array[],int max_accts)
{
ifstream infile("c:\\Programs\\Bankprogin.txt");
int count = 0;
while (infile >> acctnum_array[count]&& count < max_accts)
{infile >> balance_array[count];
count ++;
}
return count;
infile.close();
}

void print_accts(int acctnum_array[],double balance_array[],int num_accts)
{
for (int i = 0;i < num_accts;i ++)
cout << acctnum_array[i] << " " << balance_array[i] << endl;
}

int findacct(int acctnum_array[],int num_accts,int account)
{
cout << "Enter account number: " << endl;
cin >> account;
for(int i = 0; i < num_accts; i++) {
if (account == acctnum_array[i]) {
cout << "Found!";
return account;
}
else {
cout << "Not Found";
return account;
}
}

}
Last edited on
You need to have your if in a for loop,

for(int i = 0; i < num_accts; i++) {
if (account == acctnum_array[i]) {
cout << "Found!";
return account;
}
else {
cout << "Not Found";
return account;
}
}
Last edited on
So I put in that and i called the function in the main program and nothing happened. Did I call it wrong? I just put

findacct;
Your function, int findacct(int acctnum_array[],int num_accts,int account), has 3 parameters.
The first one is the array you want to look through.
The second on how many accounts are within the array.
The last one is the account to search for.

So try this in your main, (replacing the < >, with what you have called them.

int account = 0;
cout << "Enter account number: " << endl;
cin >> account;

int returnFind = findacct(<arrayName>, <sizeOfArray>, account);
That worked it now asks for a number and if I put a number in regardless what it is doesn't say "found" and it asks for another number that responds with "not found".
Last edited on
try and use the standard library algorithms wherever possible, like in this case std::find() could do the job:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    int acctnum_array[]  {123, 456, 789, 1011, 1213, 1415};

    std::cout << "Enter a/c number to search: \n";
    int n1{};
    std::cin >> n1;

    auto result1 = std::find(std::begin(acctnum_array), std::end(acctnum_array), n1);

    std::cout << ((result1 != std::end(acctnum_array)) ? "found \n" : "not found \n");
}
//http://en.cppreference.com/w/cpp/algorithm/find 

the benefit of using the standard library algorithms is that (a) they can be used alongside other standard library components like containers and iterators and (b) they are more robust having gone through the whole C++ peer-review process
Last edited on
Topic archived. No new replies allowed.