Hey all.

I'm writing a simple bank program with a menu of options, and the first option is to create an account. The user enters an account number and this number is stored in the array in the first empty slot.

So for example, if the user enters 100, it will be stored in array[0]. If the user chooses to create another account and enters 101 after that, it should be stored in array[1]. And so on. The user isn't asked how many numbers they want to enter, they're just asked to enter a number and the number should be stored in an empty array.

I'm thinking this should involve a loop, but my problem is that the loops I've tried stores the first number into every single spot in the array, so the second input doesn't work.

Here's what I've tried:

``````int accountnum[1000];
int accnum;
cout << "Enter an account number: ";
cin >> accnum;
for(int i=0;i<1000;i++)
{
if(accountnum[i]==0)
{
accountnum[i]=accnum;
}
}``````

Any help would be great. Thanks.

3
Contributors
13
Replies
14
Views
8 Years
Discussion Span
Last Post by NathanOliver

you can have the input statement inside the loop

``````int accnum;
int accountnum[1000];
for (int i = 0; i < 100; i++)
{
cout << "please enter an account number: ";
cin >> accnum
accountnum[i] = accnum;
}``````

Edited by NathanOliver: n/a

sorry didn't totally read your post. it sounds like you need to be writing a function for getting the account numbers in that case you could have the array and a variable that holds how many entries are in the array in your main function. then you could pass the array and the counter variable into the function and have it ask for the account number. then store the account number into the array at the counter + 1 and then increment the counter. in order to do this you will either have to pass the counter variable into the function by reference or as a pointer or have the function return counter + 1 and store that in counter. i hope i'm making sense with this.

In your code once you read the account number, for loop iterates for 1000 times. For the very first read array is filled with accnum (assuming you have initialised array to zero).
You must use break statement as soon as an empty slot is filled.

Thanks for the replies.

Nathan, the option to get the numbers is in a function. In the main function I have the menu displayed and then the user selects option 1, which goes to the create_account function, where they are prompted for a number that will be stored in an array. The problem is I don't know how to get it to store the number into only one empty array. I don't quite understand what you mean by the counter variable and counter+1 though. Could you clarify this?

codeguru, yeah I set everything to 0 in the main function, and I see what you mean. I'm trying to avoid using a break statement though.

well if you are using just one array to store all the account numbers then

``````void create_account(int array[1000], int * counter);

int main()
{
int * counter = 0;
int accountnum[1000];
// when the user selects to create an account
creat_account(accountnum, counter);
}

void creat_account(int array[1000], int * counter)
{
int temp;
cout << "please enter an account number: ";
cin >> temp;
array[(*counter)] = temp;
*counter++;
}``````

this will keep track of how many number are in the array and every time you need to add a number to the it will put it in the right spot and update the number of numbers are in the array.

When I try that the program crashes after input.

Is there a way to do this within a loop? Possibly with something like array[counter+1] or something?

sorry i had a coding error line 18 should be `(*counter)++` see if that fixes the problem

Edited by NathanOliver: n/a

No, it's still crashing.

I tried using passing by reference and set it up the same way but that brings me back to my original problem where the second number entered will not work.

sorry one other mistake starting at line 5

``````int *counter = new int;
*counter = 0;
//...``````

this will fix the problem.

Thank you! I think I got that working now since the numbers seem to be stored in the proper arrays. But now I'm having a problem in another part of my program.

I have 2 functions, one called find_account and another called display_balance. In display_balance, the user enters an account number and it will call the find_account function to find the correct account number. Once that has been found, it displays the balance of that account. This works fine with array[0], but anything past that won't work.

This is the find_account function:

``````int find_account(int accountnum[],int accnum,double balance[]){
for(int i=0;i<1000;i++){
if(accnum==accountnum[i])
return i;
else
return -1;
}
}``````

And the display_balance function:

``````void display_balance(int * counter,int accountnum[],double balance[]){
int accnum,i;
cout << "DISPLAY BALANCE" << endl;
cout << "Enter account number: ";
cin >> accnum;
i=find_account(accountnum,accnum,balance,interestrate);
if(i>-1)//if account number was found
cout << "Current balance: " << balance[i] << endl;
else
cout << endl << "Invalid account number." << endl;
}``````

I think the problem may be in the find_account function. It returns 0 when I enter the number stored in accountnum[1] or anything higher. But I have no idea what the problem may be.

on lines 3-6 you are checking if the accnum==accountnum and if it doesnt return -1. so one the frist time if the number isnt equal then it will return -1. i would suggest re writting the loop as

``````for (int i = 0; i < 1000; i++)
{
if (accnum == accountnum[i])
return i;
}
return -1;``````

Edited by NathanOliver: n/a

Oh, I see. Thank you so much, I appreciate it! Everything seems to be working fine now.

no problem