1

I want to write a program in c , that manage an array of structure, bankaccount with 3 attributes: 1-number of the account, 2-account balance and 3-the account owner.

i want to write a function :

  • that adds a new account whenever the user press 'a',
  • the user has to type the balance and the name of owner but the account number must be assigned automatically and be incremented whenever new account is created .

This is my code until now:

#include <stdio.h>

struct bancaccount {
  int num;
  double balance;
  char owner;
};

void maintext(); //prototype of the function that print the main text
void addaccount();

void main(void) {
  struct bankaccount account[50];

  maintext();

  char ch;
  ch = getchar();

  if (ch == 'a') {
    addaccount();
    maintext();
  }

  else if (ch == 'b') {
    printf("Result: show");
  }

  else {
    printf("another key was pressed ");
  }

}

void maintext() {
  printf("tap one of this keys : \n"
         " a : to add a new account \n"
         " b : to see all accounts\n");
}

void addaccount(struct comptebanc num) {
  num++; //this seems not possible it gives an error, what should i do insteed 
  num = num; 
  printf("owner name : ");
  scanf("%s", account.owner);
  print("balance : ");
  scanf("%lf", account.balance);

  printf("\n\n");
  printf("Num : %d\n", account.num);
  printf("Prop : %s\n", account.owner);
  printf("Solde : %lf\n", account.balance);
}

How can i assign a number to every new account ? and how can i save new element in the array of the struct ? thank you for your help.

i'm still a beginner so i'm sure i made some mistakes in the fundamentals o c

6
  • Perhaps you meant numero.num++ (or possibly accound.num++)? Commented Dec 29, 2018 at 14:41
  • yeah sorry this was a typo, i mean account.num++ but still gives me an error of undeclared (first used in this function) Commented Dec 29, 2018 at 14:48
  • 1
    Be aware: you cannot scanf("%s", account.owner); on char owner; Commented Dec 29, 2018 at 15:07
  • @duong_dajgja can you please tell me why ? Commented Dec 29, 2018 at 15:22
  • 2
    @ATrihop scanf("%s", account.owner); is to store a string (char array essentially) while char owner; is simply a single char. Commented Dec 29, 2018 at 15:31

3 Answers 3

2

Here is how you might wana go about this.

#include <stdio.h>

typedef struct {
  int num;
  double balance;
  char owner[15];
} bankaccount;

int main() {
  int numAllocated = 0; // Number of accounts allocated
  bankaccount accounts[50];

  int ch;
  printf("\"a\": to add new account\n\"b\": to print all accounts\n");

  void addAccount (bankaccount *, int);
  void printAccounts (bankaccount *, int);
  for ( ;; ) {
    ch = getchar();
    if (ch == '\n' ) continue;
    else if (ch == 'a') {
      addAccount(accounts, numAllocated);
      numAllocated++;
    }
    else if (ch == 'b') printAccounts(accounts, numAllocated);
    else break;
    printf("\"a\": to add new account\n\"b\": to print all accounts\n");
 }
}

void addAccount (bankaccount *account, int num) {
  account[num].num = num + 1;
  printf("Owner's name: ");
  scanf("%s", account[num].owner);
  printf("Balance: ");
  scanf("%lf", &account[num].balance);
}

void printAccounts (bankaccount *accounts, int num) {
  if (num > 0) {
    for (int i = 0; i < num; i++) {
      printf("\n%-10s%d\n", "Acc#:", accounts[i].num);
      printf("%-10s%s\n", "Name:", accounts[i].owner);
      printf("%-10s%.2f\n", "Balance:", accounts[i].balance);
    }
  }
  else printf("No account allocated yet\n");
}

I went by your code sample. Though you might wana make this better by reading about dynamic memory, pointers and probably linked list to make you account list and owner field of the struct dynamic. But here is start and hope you can take it from here!

Sign up to request clarification or add additional context in comments.

Comments

1

Dynamic array tag is rather confusing as you have only the static ones.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  int num;
  double balance;
  char *owner;
} bancaccount;

size_t Naccounts = 0;
bancaccount *accounts = NULL;

bancaccount addaccount(int num, double ballance, char *owner)
{
    bancaccount *tmp = realloc(accounts, ++Naccounts * sizeof(*tmp));

    if(tmp)
    {
        accounts = tmp;
        tmp[Naccounts - 1].owner = malloc(strlen(owner) + 1);
        if([Naccounts - 1].owner)
        {
            strcpy(tmp[Naccounts - 1].owner, owner);
            tmp[Naccounts - 1].balance = ballance;
            tmp[Naccounts - 1].num = num;
        }
    }
    return tmp;
}

1 Comment

thank you for your answer, but can you please give me some instructions on how to use this to reach my goal ? i tried many times and got some errors. I'm still a beginner student. thank you.
1

Here is another working solution, based on your code. But I use the scanf_s function instead of scanf in this example.

#include <stdio.h>

struct bancaccount {
    int num;
    double balance;
    char owner[64];
};

struct bancaccount accounts[10];
int account_id = 0;

void maintext(void)
{
    printf("tap one of this keys : \r\n"
        " a : to add a new account \r\n"
        " b : to see all accounts\r\n");
}

void add_account(void)
{
    accounts[account_id].num = account_id + 1;
    printf("owner name : ");
    scanf_s("%63s", &accounts[account_id].owner, 64);
    printf("balance : ");
    scanf_s("%lf", &accounts[account_id].balance);

    printf("\r\n\r\n");
    printf("Num : %d\r\n", accounts[account_id].num);
    printf("Prop : %s\r\n", accounts[account_id].owner);
    printf("Solde : %lf\r\n", accounts[account_id].balance);
    account_id++;
}

void print_accounts(void)
{
    int i;

    for (i = 0; i < account_id; i++) {
        printf("Num : %d\r\n", accounts[i].num);
        printf("Prop : %s\r\n", accounts[i].owner);
        printf("Solde : %lf\r\n", accounts[i].balance);
        printf("\r\n");
    }       
}

int main(int argc, char *argv[])
{
    char c;

    maintext();

    while (1) {
        c = getchar();

        switch (c) {
        case 'a':
            add_account();
            getchar(); /* consume '\n' */
            break;
        case 'b':
            print_accounts();
            getchar(); /* consume '\n' */
            break;
        case 'q':
            return 0;
        default:
            printf("another key was pressed\r\n");
        }
    }

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.