0

Create a structure to specify the data for customers in a bank. The data to be stored is: Account number, Name, Balance in account. Assume a maximum of 200 customers in the bank.

struct CustomerData {
   int   acNum;
   float balance;
   char name[];
} n[2];

void main() {
    for(int i = 0;i<2; i++) {
        printf("give Ac. no. of %d customer\n",i+1);
        scanf("%d",&n[i].acNum);
        printf("balance of customer %d\n",i+1);
        scanf("%f",&n[i].balance);
        printf("Name of customer %d\n",i+1);
        fflush(stdin);
        gets(n[i].name);
    }

    printf(" Name      Acc. no    Balance \n");

    for(int i =0;i<2;i++) {
        printf("%c      %d           %f\n",puts(n[i].name),n[i].acNum,n[i].balance);
    }
}

Output:

give Ac. no. of 1 customer
50054
balance of customer 1
11316
Name of customer 1
sahil
give Ac. no. of 2 customer
15655
balance of customer 2
100
Name of customer 2
Rishav
 Name      Acc. no    Balance
'=
       50054           11316.000000
Rishav
       15655           100.000000

Process returned 34 (0x22)   execution time : 25.120 s
Press any key to continue.
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 5
    `fflush` on input streams invokes undefined behavior, and `gets(n[i].name);` is not only wrong because it's using a function no longer in the standard library, it's worse because it's targeting a flexible array member that has no "there" there. Whatever book/site is teaching this, burn it. – WhozCraig Jul 21 '21 at 09:36
  • 2
    Why do you print the result of `puts`? It seems you have missed some crucial parts of the early chapters of your beginners text-book. And if you don't have a book, please get one. – Some programmer dude Jul 21 '21 at 09:36
  • And [the flexible array member](https://en.wikipedia.org/wiki/Flexible_array_member) `name` in your structure isn't a dynamic array. There's no space allocated for it. You really need to get a hold of a decent beginners book and start reading it from the very beginning. And whatever resource you currently use to learn C, throw it away. – Some programmer dude Jul 21 '21 at 09:37
  • Sahil Dadhwal, what is the longest name allowed? [600+ letters](https://en.wikipedia.org/wiki/Hubert_Blaine_Wolfeschlegelsteinhausenbergerdorff_Sr.)? – chux - Reinstate Monica Jul 21 '21 at 13:01
  • 2
    Sahil Dadhwal, I recommend to drop `scanf()`. Use `fgets()` to read each line of user input into a string and then parse the string. – chux - Reinstate Monica Jul 21 '21 at 13:04
  • You're supposed to allow up to 200 customers; your code insists on exactly 2 and would (probably) crash if any more were added. Also, consistent indentation is a great help to everyone — especially people learning to read C code. See also [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/q/1694036/15168) – Jonathan Leffler Jul 21 '21 at 16:40

1 Answers1

0

As mentionned in the comments, there are a couple of problems in your code:

  1. Never do fflush(stdin). It causes an undefined behaviour.
  2. Never use scanf() to read input. Use fgets() instead.
  3. You should indent your code properly so it becomes easier to read.

Here is a better version of your code:

#include <stdio.h>
#include <string.h> // for strcspn()

struct CustomerData {
    int acNum;
    float balance;
    char name[255]; // Give it a size
};

We need to define a function that reads one line from the input (remember not to use scanf()):

char *readstr(char *str, int size)
{
    if (!fgets(str, size, stdin)) {
        fprintf(stderr, "Input error\n");
        return NULL;
    }
    
    str[strcspn(str, "\n")] = '\0'; // fgets() reads the ending '\n' so null-terminate the string
    return str;
}

You can do the same with ints and floats:

int readint(int *i)
{
    char buffer[255];
    if (!fgets(buffer, 255, stdin)) {
        fprintf(stderr, "Input error\n");
        return 0; // failed
    }

    buffer[strcspn(buffer, "\n")] = '\0';
    
    if (sscanf(buffer, "%d", i) != 1)
        return 0; // failed
    
    return 1; // success
}

int readfloat(float *f)
{
    char buffer[255];
    if (!fgets(buffer, 255, stdin)) {
        fprintf(stderr, "Input error\n");
        return 0; // failed
    }

    buffer[strcspn(buffer, "\n")] = '\0';
        
    if (sscanf(buffer, "%f", f) != 1)
        return 0; // failed
        
    return 1; // success
}

Now, you can use those functions in your main():

int main() // main() should always return an int
{
    const int num_customers = 2; // In case you wanted to change it later
    struct CustomerData n[num_customers];
    
    for(int i = 0; i < num_customers; i++) {
        printf("give Ac. no. of %d customer: ", i+1);
        readint(&n[i].acNum);
        
        printf("balance of customer %d: ", i+1);
        readfloat(&n[i].balance);
        
        printf("Name of customer %d: ", i+1);
        readstr(n[i].name, sizeof(n[i].name));
    }
    
    printf("\nName\tAcc. no\tBalance\n");
    
    for(int i = 0; i < num_customers; i++)
        printf("%s\t%d\t%f\n", n[i].name, n[i].acNum, n[i].balance);
}

An example output:

give Ac. no. of 1 customer: 1
balance of customer 1: 12
Name of customer 1: john
give Ac. no. of 2 customer: 2
balance of customer 2: 65.5
Name of customer 2: wick

Name    Acc. no Balance
john    1       12.000000
wick    2       65.500000
Zakk
  • 1,935
  • 1
  • 6
  • 17