Originally Posted by
Salem
It's better if you post code between code tags, instead of fuzzy pictures.
It's better when you post code between code tags, that you "paste as text", rather than the washed out colour scheme of your IDE.
What is the size of your bank array?
What value is numberofAccounts when you call this function.
A copy/paste of your console showing what you typed is also useful.
Okay thanks. The bank array is set to 100 and numberofaccounts is initialized as 0.
Here is the entire code so far:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000;
#define charachter 30;
struct Bank
{
char surName[30];
char lastName[30];
char accountNumber[6];
int balance;
};
typedef struct Bank bank;
int registerNewAccount(bank b[], int numberofAccounts);
void printAllAccounts(bank b[], int numberofAccounts);
void sortAccounts(bank b[]);
void changeBalance(bank b);
int menu(bank b[], int numberofAccounts);
void saveToFile(bank b[]);
void readFromFile(bank b);
bank createaccount(char accountnumber[], char surname[], char lastname[], int balance);
int main(void)
{
int numberofAccounts = 0;
bank b[100];
printf("Bank...\n\n");
numberofAccounts = menu(b, numberofAccounts);
}
int menu(bank b[], int numberofAccounts)
{
int val, a[1000] = {0}, size;
size = sizeof(a) / sizeof(a[0]);
for (;;)
{
printf(
"\n"
"(1) Register new account\n"
"(2) Show all accounts\n"
"(9) Exit\n"
"Ange val:\n");
scanf("%d", &val);
switch (val)
{
case 1:
numberofAccounts = registerNewAccount(b, numberofAccounts);
break;
case 2:
printAllAccounts(b, numberofAccounts);
printf("Number of accounts = %d\n", numberofAccounts);
break;
default:
break;
}
}
}
int registerNewAccount(bank b[], int numberofAccounts)
{
char accountnumber[30], surname[30], lastname[30];
int balance;
char quit[] = "q";
printf("Number of accounts: %d", numberofAccounts);
for (;;)
{
printf("Register new Account\n");
printf("Assign a new account number (6 digits) (q to quit): ");
do
{
scanf("%s", accountnumber);
if (strcmp(accountnumber, quit) == 0)
{
printf("You have choosen quit");
return 0;
}
} while (strlen(accountnumber) < 6);
printf("Assign a name surname and lastname: ");
scanf("%s %s", surname, lastname);
balance = 0;
b[numberofAccounts] = createaccount(accountnumber, surname, lastname, balance);
numberofAccounts++;
}
return numberofAccounts;
}
bank createaccount(char accountnumber[], char surname[], char lastname[], int balance){
bank b;
strcpy(b.accountNumber, accountnumber);
strcpy(b.surName, surname);
strcpy(b.lastName, lastname);
b.balance = balance;
return b;
}
And this is what the console says:
Bank...
(1) Register new account
(2) Show all accounts
(9) Exit
Choose:
1
Number of accounts: 0
Register new Account:
Assign a new account number (6 digits) (q to quit): 123456
Assign a name surname and lastname: hello world
[1] 43041 illegal hardware instruction
But when i comment out the line "b[numberofAccounts] = createaccount(accountnumber, surname, lastname, balance);"
which is in the function registerNewAccount, everything is fine.