1

I made a program to take multiple inputs as string, store and print them. Somehow the "Account Number" that's stored in a[i].acn is not printing. I've tried debugging and the problem seems to be in the loop that adds spaces to a[i].name .

#include <stdio.h>
#include <string.h>
struct Bank
{
    char name[10],acn[8];
    float bal;
}a[100];
int n,i,flag;
void add()
{
//function to add Rs. 100 to accounts that have balance over 1000
//and print the details.
    for(i=0;i<n;i++)
        if(a[i].bal>=1000)
            a[i].bal+=100;
    printf("\nS.No.\tName\t\tAcc. No.\tBalance(in Rs.)");
    for(i=0;i<n;i++)
        printf("\n[%d]\t%s\t%s\t%.2f",i+1,a[i].name,a[i].acn,a[i].bal);
}
void main()
{
    printf("Enter the number of customers: ");
    scanf("%d",&n);
    printf("Enter the details of %d customers:\n",n);
    for(i=0;i<n;i++)
    {
        printf("\nCustomer-%d",i+1);
        printf("\nFirst Name: ");
        fflush(stdin);
        gets(a[i].name);
        printf("Account Number: ");
        fflush(stdin);
        gets(a[i].acn);
        printf("Account Balance: ");
        scanf("%f",&a[i].bal);
    }
    for(i=0;i<n;i++)//The problem seems to be in this loop
        while(strlen(a[i].name)<10)
            strcat(a[i].name," ");
    add();
}

Input:

Enter the number of customers: 2
Enter the details of 2 customers:

Customer-1
First Name: Aarav
Account Number: ASDF1234
Account Balance: 1200

Customer-2
First Name: Asd
Account Number: abcd1122
Account Balance: 999.9

Output:

S.No.   Name            Acc. No.        Balance(in Rs.)
[1]     Aarav                   1300.00
[2]     Asd                     999.90
oguz ismail
  • 1
  • 16
  • 47
  • 69
Aarav
  • 13
  • 3

3 Answers3

1

There are few things to correct in your program.

  1. Use fgets instead of gets because in gets there is no boundary check and there is a danger of reading beyond the allocated size.

  2. When using fgets , scanf and even gets they all read the left over \n characters from the standard buffer , so using fgets we can avoid it if we properly use it.(scanf also avoids space characters but it cannot be used to read multiword strings)

  3. remove fflush(stdin), its not required you can see reason here

  4. And last, but certainly not least use int main() instead of void main()

IrAM
  • 1,720
  • 5
  • 18
  • 1
    Point 2 is incorrect in several ways, e.g. that `scanf` can't read multi-word strings - it can. – Support Ukraine Dec 20 '20 at 07:30
  • @4386427, please mention those, will add to the answer, end goal is to help. – IrAM Dec 20 '20 at 07:33
  • 1
    "scanf ... but it cannot be used to read multiword strings" --> `scanf("%*[^\n]%*1[\n]" "%*[^\n]%*1[\n]" "%*[^\n]%*1[\n]" );` will nicely read in 3 non-empty lines of input. Still, using `fgets()` is a _good_ and preferable idea. – chux - Reinstate Monica Dec 20 '20 at 07:36
  • @chux-ReinstateMonica, this is good to know, then i should change my answer to _it cannot read in a straight forward way (just using `%s`) unless we use few list of escape sequence characters._ – IrAM Dec 20 '20 at 07:41
  • Thanks everyone for your answer and helpful comments! – Aarav Dec 20 '20 at 07:59
1

You have this:

struct Bank
{
    char name[10],acn[8];
    float bal;
}a[100];

since C strings must end with a NUL character (aka '\0') the name can be at maximum 9 chars long.

But here

    while(strlen(a[i].name)<10)
        strcat(a[i].name," ");

you keep adding spaces until it's 10 characters long. In other word - you write outside the array.

Change name to be name[11]

Besides that gets and fflush(stdin) should be avoided.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1

scanf() then gets() is bad

scanf("%d",&n); reads the integer, yet leaves the following '\n' in stdin that gets() reads as an empty string "".

I recommend to use fgets() to read all the line into a sizable buffer and then parse using sscanf(), strtol() etc.


Code overfills buffer

a[i].nam only has enough room for 9 characters and then the null character. Appending a space until it has a length more the 9 overfills .name[10].

struct Bank {
  char name[10],acn[8];
  float bal;
} a[100];

while(strlen(a[i].name)<10)
   strcat(a[i].name," ");

Use while(strlen(a[i].name)<9) to pad with spaces, but not too many.


Money needs special considerations. For now, consider a long of lowest denominational (cents). Read in a double and then long balance = lround(input*100.0);

Other weaknesses exist.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256