2
\$\begingroup\$

I am new to C. I tried to make a bank management console application. I want some one to review it for advice to enhance and develop my coding skills.

This is what the program does with the inputs:

  • 1 add an account
  • 2 search for a specific account
  • 3 modify a specific account
  • 4 delete a specific account

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

//part of declaration of the types      

typedef struct date
{
  int j,m,a;//j:day/m:month/a:year
}date;

typedef struct cus //cus:client
{
    char  name[50],prenom[50],adr[50],tel[50];//adr: adresse/prenom:name/name:family name
    int f;// f:ID
    date datec;//date:date of birth

}cus;

typedef struct
{
  int ic,solde;//ic:ID_Client/solde:how much money in the account
  char  tc;//tc:Type_Compte
  bool b;//b:Bloqed or opened
}account;

cus t[10];//cus is for costumer
account v[10];

//part of the functions or procedures
       //part of search functions and procedures

int msearchclient()
{
    cus cuss;
    int d=32,k,*a,p;
    printf("enter the ID of the costumer:");
    scanf("%d" ,&cuss.f);
     for(int i=0;i<10;i++)
      {
        if(cuss.f==t[i].f)
         {
          k=1;
          printf("\n");
          printf("\t\tFOUND\n\n");
          a=&t[i].f;
          p=*a;
         }
      }

      if(k!=1)
        printf("\t\tNOT FOUND\n\n");

    return p;

}

void mshowclient()
{
    int d=32,a=msearchclient();

    for(int i=0;i<10;i++)
         {
          if(a==t[i].f)
           {
          printf("|FULL Name\t\t|ID\t\t|Adresse\t\t|Telphone Number\t\t|Date of Birth\n");
          printf("|%s%c%s \t\t|%d\t\t|%s\t\t|%s\t\t|%d/%d/%d\n" ,t[i].name,d,t[i].prenom,t[i].f,t[i].adr,t[i].tel,t[i].datec.j,t[i].datec.m,t[i].datec.a);

          }
         }
}


int msearchaccount()
{
    account accountt;
    int k,*a,p;

    printf("enter the ID of account:");
    scanf("%d" ,&accountt.ic);
    for(int i=0;i<10;i++)
    {
         if(accountt.ic==v[i].ic)
         {
             k=1;
             printf("\n");
             printf("\t\tFOUND\n\n");
             a=&v[i].ic;
             p=*a;

         }
    }
    if(k!=1)
        printf("\t\tNOT FOUND\n\n");

    return p;
}


void mshowaccount()
{
    int a=msearchaccount();
    for(int i=0;i<10;i++)
      {
         if(a==v[i].ic)
         {
            printf("|Account ID\t\t|Type of account\t\t|Balance\t\t|Account Opened(1) or closed(0)\n");
            printf("|%d\t\t|%c\t\t|%d\t\t|%d\n" ,v[i].ic,v[i].tc,v[i].solde,v[i].b);
         }
      }
}

void msearch()
{

  int x;
  printf("\n\n");
  printf("Do you want search :\n=>>(1)the client \n=>>(2)the account \n=>>(3)Account and client informations \n=>>(4)Back \n=>>");
  scanf("%d" ,&x);
  switch (x)
  {

  case 1:
    {

        msearchclient();

    }break;

   case 2:
       {
           msearchaccount();

       }break;

   case 3:
    {
        mshowclient();
        mshowaccount();
    }break;

   case 4:
    {
        MENU();
    }break;

   default :{printf("enter another number please\n");msearch();}
  }

}


       //part of delete function

void mdelete()
{

  account accountt;
  cus cuss;
  int x;
  printf("\n\n");
  printf("Do you want delete :\n=>>the client (1)\n=>>the account (2)\n=>>Back (3)\n=>>");
  scanf("%d" ,&x);
  switch (x)
  {

  case 1:
    {

         printf("enter the id of the client:");
         scanf("%d" ,&cuss.f);
         int k;

      for(int i=0;i<10;i++)
         {
          if(cuss.f==t[i].f)

            {

            t[i].name[50]=NULL;
            t[i].prenom[50]=NULL;
            t[i].datec.a=NULL;
            t[i].datec.j=NULL;
            t[i].datec.m=NULL;
            t[i].adr[50]=NULL;
            t[i].tel[50]=NULL;
            k=1;
            }
          }
          int i,d=32;
      if(k==1)
        printf("GOOD we deleted it");


    }break;

   case 2:
       {
            printf("enter the id of the account:");
            scanf("%d" ,&accountt.ic);
            int k;

      for(int i=0;i<10;i++)
         {
          if(accountt.ic==v[i].ic)

            {
            v[i].ic=NULL;
            v[i].solde=NULL;
            v[i].tc=NULL;
            v[i].b=NULL;
            k=1;
            }
          }
     if(k==1)
        printf("GOOD we deleted it");

       }break;
    case 3:
    {

        MENU();
    }
   default :{printf("enter another number please\n");mdelete();}
  }



}




     //part of modify functions and prosedures


void mmmenuodify()
{
    int n;
    account accountt;
    printf("\n");
    printf("what do you want to modify in the account:\n");
    printf("\n\n");
    printf("1- the amount of money\n");
    printf("2- the type of the account\n");
    printf("3- the id of account\n");
    printf("4- Blocking the account\n");
    printf("5- Extract the money\n");
    printf("6- Deposite the money\n");
    printf("7- Back\n");
    printf("\n=>>");

    scanf("%d",&n);

    switch (n)
    {
        case 1:
          {
            printf("enter the new amount of money");
            scanf("%d" ,&accountt.solde);


            for(int i=0;i<10;i++)
             {
                if(msearchaccount()==v[i].ic)
                   {
                    v[i].solde=accountt.solde;
                    msearchaccount();
                   }

             }
             msearchaccount();
          }break;

        case 2:
          {
            printf("enter the new type:");
            scanf("%c" ,&accountt.tc);

            for(int i=0;i<10;i++)
              {
                if(msearchaccount()==v[i].ic)
                   v[i].tc=accountt.tc;
              }
          }break;

        case 3:
          {
             printf("enter the new id of account:");
             scanf("%d" ,&accountt.ic);

             for(int i=0;i<10;i++)
              {
               if(msearchaccount()==v[i].ic)
                 v[i].ic=accountt.ic;
              }
          }break;

        case 4:
            {
                int n;
                printf("enter the number:\n=>>(0) for blocking the account\n=>>(1) for opening he account\n=>>");
                scanf("%d" ,&n);
                if(n==0)
                {

                    for(int i=0;i<10;i++)
                       {
                         if(msearchaccount()==v[i].ic)
                            v[i].b=0;
                       }
                }


                else if(n==1)
                {

                    for(int i=0;i<10;i++)
                       {
                         if(msearchaccount()==v[i].ic)
                            v[i].b=1;
                       }
                }

            }

        case 5:
          {
             printf("enter how much do you want to take:");
             scanf("%d" ,&accountt.solde);

             for(int i=0;i<10;i++)
              {
               if(msearchaccount()==v[i].ic)
                 {
                 if(v[i].solde>0)
                    v[i].solde=v[i].solde-accountt.solde;
                 else
                 {
                     printf("you can't take money because you don't have money in the account");
                     break;
                 }
                 }

              }

          }break;

        case 6:
          {
               printf("enter how much do you want to deposite:");
             scanf("%d" ,&accountt.solde);

             for(int i=0;i<10;i++)
              {
               if(msearchaccount()==v[i].ic)
                 v[i].solde=v[i].solde+accountt.solde;
              }

          }break;

        case 7:
          {
               printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
               MENU();
          }break;

        default: {printf("enter new number");mmmenuodify();}
    }

}

void mmodify()
{
    mmmenuodify();

}

    //part of create function

void mcreate()
{
     int x;
     printf("\n");
     printf("how many costumers you want to add:");
     scanf("%d" ,&x);

     //FILE *pr=fopen("tahiro.txt" ,"w+");

            for(int i=0;i<x;i++)
              {

                printf("enter the full name:" );
                scanf("%s %s" ,&t[i].prenom,&t[i].name);//when you enter the full name you have to make space between the name and family name and in the same family name no spaces

                printf("enter thier ID:");
                scanf("%d" ,&t[i].f);



                printf("enter the date of birth:");
                scanf("%d/%d/%d" ,&t[i].datec.j,&t[i].datec.m,&t[i].datec.a);

                printf("enter the telephone number:");
                scanf("%d" ,&t[i].tel);

                printf("enter Adresse please:");
                scanf("%s" ,&t[i].adr);

                printf("\n\n");
                printf("Now for the account informations");
                printf("\n\n");

                printf("enter the account ID please:");
                scanf("%d" ,&v[i].ic);

                int a=2020-t[i].datec.a;


                if(a>=18)
                {
                    printf("------------------------------\n");
                    printf("Enter the type of the account:\n=>>'P' for particular account\n=>>'C' for commercial\n");


                    getchar();
                    scanf("%c",&v[i].tc);

                    printf("------------------------------\n");
                }
                else if(a>0&&a<18)
                    v[i].tc='M';


               printf("enter the amount of money:");
                scanf("%d" ,&v[i].solde);

                printf("Block or Not:");
                scanf("%d" ,&v[i].b);

                //fprintf(pr,"%s\t\t%s\t\t%d\t\t%d\t\t%s\t\t%d/%d/%d\t\t%c\n" ,t[i].name,t[i].prenom,t[i].f,t[i].tel,t[i].adr,t[i].datec.j,t[i].datec.m,t[i].datec.a,v[i].tc);
                if(x>1)
                {
                    printf("do you want to continue(Y/N):\n=>>");
                    if(getchar()=='N'||getchar()=='n')
                       break;
                }



                printf("\n\n");
              }

              //fclose(pr);
}

     //part of menu function


void MENU()
{
    int n;
    printf("\t\tHELLO AND WELCOME");
    printf("\n\n\n");

    printf("1-Add an account\n");
    printf("2-Find an account\n");
    printf("3-Modify an account\n");
    printf("4-Delete an account\n");
    printf("\n\n\n");
    printf("chosse the number for the service:");
    scanf("%d" ,&n);
    printf("\n");
    switch (n)
        {
            case 1:
            {
                printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                mcreate();
                printf("\n\n\n");
                printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                MENU();
            }break;

            case 2:
            {
                printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                msearch();
                printf("\n\n\n");
                printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                MENU();
            }break;

            case 3:
            {
                printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                mmodify();
                printf("\n\n\n");
                printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                MENU();
            }break;

            case 4:
            {
                printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                mdelete();
                printf("\n\n\n");
                printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                MENU();
            }break;
        }
}


   //main part

void main()
{
    system("color 0A");
    MENU();
}
\$\endgroup\$
  • \$\begingroup\$ @Taher Rezzag Unfortunately your code doesn't even compile. Fix that first please before coming back here to ask about improvements. (BTW use the word customer please, a costumer is something completely different) \$\endgroup\$ – πάντα ῥεῖ Feb 22 at 11:43
  • \$\begingroup\$ @πάντα ῥεῖ Are you compiling with C++ or C? I get "warning: implicit declaration of function 'MENU' [-Wimplicit-function-declaration]", you got "error: 'MENU' was not declared in this scope", yet with file named "main.cpp", I am suspicious the of what compiler was used. \$\endgroup\$ – chux - Reinstate Monica Feb 22 at 14:40
  • \$\begingroup\$ @chux As you can clearly see at the link, I was compiling with the following command: gcc main.cpp without any further specific options. The compiler used was gcc. \$\endgroup\$ – πάντα ῥεῖ Feb 22 at 15:34
  • 1
    \$\begingroup\$ @chux-ReinstateMonica I'm using CLion on top of Visual Studio 2019 and it doesn't compile. Among other error messages some of the scanfs are using a numeric format to read strings. \$\endgroup\$ – pacmaninbw Feb 22 at 16:18
  • 1
    \$\begingroup\$ @pacmaninbw I am unfamiliar with "CLion on top of Visual Studio 2019". Does that constitute a C compilation? My C compilation gave many warnings, yet no errors. What error did you receive? "scanfs are using a numeric format to read strings." is certainly bad beginner code, yet is not an error, but UB. Of course many do compile with warnings treated as errors. \$\endgroup\$ – chux - Reinstate Monica Feb 22 at 17:47
3
\$\begingroup\$

or advice to enhance and develop my coding skills.

Enable all warnings

I received many with "gcc -std=c11 -O0 -g3 -pedantic -Wall -Wextra -Wconversion -c -fmessage-length=0 ". See list below.

Save time and let the automatic checking of a good well enabled compiler provide you feedback.

Weak input

All input is through scanf(). That consistency is good, yet scanf() is very difficult to use and handle unexpected input. Use fgets() to read a line of user input into a string. Then parse the string.

Classic missing space with %c

Code's usage of scanf() leaves the Enter or '\n' in stdin. The "%c" in scanf("%c", &accountt.tc);, unlike "%s" and "%d", does not consume and toss leading white space. When case 2 is not used first, scanf("%c" will read a '\n'.

  // scanf("%c", &accountt.tc);
  scanf(" %c", &accountt.tc);

Check return values of input

The return value of scanf() is not checked.

Cope with names better

Real names can have spaces in the first name. Real names can have spaces in the last name. Real names can exceed 49 letters.

Use width limits with %s

Do not use %s without a width. %s --> %49s for code's name[50].

Avoid (naked) magic numbers

Example: Instead of name[50]

#define NAME_MAX_SIZE 50

char name[NAME_MAX_SIZE];

Auto formatting

Many coding environments have an auto formatter. Post code is unnecessarily difficult to read. Improve format with tools - do not waste time doing it manually.

Spell checker

Deposite --> Deposit


../test.c:34:7: warning: unused variable 'd' [-Wunused-variable]
   int d = 32, k, *a, p;
       ^
../test.c: In function 'msearch':
../test.c:132:7: warning: implicit declaration of function 'MENU' [-Wimplicit-function-declaration]
       MENU();
       ^~~~
../test.c: In function 'mdelete':
../test.c:168:25: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           t[i].name[50] = NULL;
                         ^
../test.c:169:27: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           t[i].prenom[50] = NULL;
                           ^
../test.c:170:24: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           t[i].datec.a = NULL;
                        ^
../test.c:171:24: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           t[i].datec.j = NULL;
                        ^
../test.c:172:24: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           t[i].datec.m = NULL;
                        ^
../test.c:173:24: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           t[i].adr[50] = NULL;
                        ^
../test.c:174:24: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           t[i].tel[50] = NULL;
                        ^
../test.c:178:14: warning: unused variable 'd' [-Wunused-variable]
       int i, d = 32;
              ^
../test.c:178:11: warning: unused variable 'i' [-Wunused-variable]
       int i, d = 32;
           ^
../test.c:194:19: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           v[i].ic = NULL;
                   ^
../test.c:195:22: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           v[i].solde = NULL;
                      ^
../test.c:196:19: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           v[i].tc = NULL;
                   ^
../test.c: In function 'mcreate':
../test.c:362:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[50]' [-Wformat=]
     scanf("%s %s", &t[i].prenom, &t[i].name); //when you enter the full name you have to make space between the name and family name and in the same family name no spaces
            ~^      ~~~~~~~~~~~~
../test.c:362:16: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[50]' [-Wformat=]
     scanf("%s %s", &t[i].prenom, &t[i].name); //when you enter the full name you have to make space between the name and family name and in the same family name no spaces
               ~^                 ~~~~~~~~~~
../test.c:371:13: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'char (*)[50]' [-Wformat=]
     scanf("%d", &t[i].tel);
            ~^   ~~~~~~~~~
../test.c:374:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[50]' [-Wformat=]
     scanf("%s", &t[i].adr);
            ~^   ~~~~~~~~~
../test.c:401:13: warning: format '%d' expects argument of type 'int *', but argument 2 has type '_Bool *' [-Wformat=]
     scanf("%d", &v[i].b);
            ~^   ~~~~~~~
../test.c: At top level:
../test.c:418:6: warning: conflicting types for 'MENU'
 void MENU() {
      ^~~~
../test.c:132:7: note: previous implicit declaration of 'MENU' was here
       MENU();
       ^~~~
../test.c:472:6: warning: return type of 'main' is not 'int' [-Wmain]
 void main() {
      ^~~~
../test.c: In function 'mdelete':
../test.c:208:7: warning: this statement may fall through [-Wimplicit-fallthrough=]
       MENU();
       ^~~~~~
../test.c:210:5: note: here
     default: {
     ^~~~~~~
../test.c: In function 'mmmenuodify':
../test.c:275:13: warning: this statement may fall through [-Wimplicit-fallthrough=]
     case 4: {
             ^
../test.c:298:5: note: here
     case 5: {
     ^~~~
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ There are 2 VTC, one of those may have been a DV. \$\endgroup\$ – pacmaninbw Feb 22 at 19:05
  • \$\begingroup\$ thanks for everyone how gived me advices to enhance my coding skills it was nice to ask people like you . \$\endgroup\$ – Taher Rezzag Feb 23 at 11:37
-2
\$\begingroup\$

You are omitting to pass parameters to functions, that code will never compile.

For example: The function int msearchclient() use the array t[i] but you never pass it as an argument.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ "You are omitting to pass parameters to functions, that code will never compile." That's nonsense. cus t[10]; is a global variable definition. Thus it would be accessible from any function body. Also don't answer off-topic questions here please. \$\endgroup\$ – πάντα ῥεῖ Feb 22 at 12:46
  • \$\begingroup\$ no it would the function use the array t[10] with out declaring the parameters because i did't work with files to store data so i declared the array t[10] as const so i can work with it when i ever i want \$\endgroup\$ – Taher Rezzag Feb 22 at 14:42
  • 1
    \$\begingroup\$ @πάνταῥεῖ He doesn't have enough points to see VTC. The answer should be don't use global variables rather than it won't compile. \$\endgroup\$ – pacmaninbw Feb 22 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.