2

I have tried this code for printing some persons' basic information. Here is the code :

#include <stdio.h>

struct person {
    char name[100];
    int age;
    float salary;
};

int main() {
    struct person p[3];
    int i;
    for(i=0;i<3;i++){
        printf("Enter informations for person no %d:\n\n",i+1);
        printf("Enter name : \n");
        gets(p[i].name);
        printf("Enter age : \n");
        scanf("%d",&p[i].age);
        printf("Enter salary : \n");
        scanf("%f",&p[i].salary);
    }
    for(i=0;i<3;i++){
        printf("\n\nInformations for person no %d:\n\n",i+1);
        printf("Name: %s\n",p[i].name);
        printf("Age : %d\n",p[i].age);
        printf("Salary : %0.2f\n",p[i].salary);
    }
    return 0;
}

Now while scanning data, this code takes only takes all the information of first person and doesn't take character type data of others.

Adrian Mole
  • 43,040
  • 110
  • 45
  • 72
Maria
  • 67
  • 1
  • 3
  • 7

2 Answers2

1
  1. I would recommend using fgets() instead of gets() because gets can be dangerous (In order to use gets() safely, you have to know exactly how many characters you will be reading, so that you can make your buffer large enough). fgets() also takes the '\n' char at the end, in order to prevent this you can write the following line: p[i].name[strlen(p[i].name)-1] = '\0';

  2. Your main problem is that salary input leaves '\n' character, and you need to collect it with getchar();

So you need to write something like this:

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

struct person {
    char name[100];
    int age;
    float salary;
};

int main() {
    struct person p[3];
    
    int i;
    for(i=0;i<3;i++){
        printf("Enter informations for person no %d:\n\n",i+1);
        printf("Enter name : \n");
        fgets(p[i].name,100,stdin);
        p[i].name[strlen(p[i].name)-1] = '\0';
        printf("Enter age : \n");
        scanf("%d",&p[i].age); 
        printf("Enter salary : \n");
        scanf("%f",&p[i].salary); 
        getchar();
    }
    
    for(i=0;i<3;i++){
        printf("\n\nInformations for person no %d:\n\n",i+1);
        printf("Name: %s\n",p[i].name);
        printf("Age : %d\n",p[i].age);
        printf("Salary : %0.2f\n",p[i].salary);
    }
    
    return 0;
}
Majkl
  • 154
  • 1
  • 7
-2

The fseek() would work for you:

fseek(stdin, 0, SEEK_END)

And note that gets() shouldn't be used, it's no longer a part of C standard. Try fgets() rather.

Code structure redefined:

#include <stdio.h>

// Don't use magical numbers, use constants in case
// the value is not going to be changed
#define MAX 100

// ...

int main(void) {
    struct person p[3];

    for (int i = 0; i < 3; i++) {
        printf("Enter informations for person no %d:\n\n", i + 1);

        printf("Enter name : \n");

        // this will prevent skipping the input
        fseek(stdin, 0, SEEK_END); 
        fgets(p[i].name, MAX, stdin);

        printf("Enter age : \n");
        scanf("%d", &p[i].age);

        printf("Enter salary : \n");
        scanf("%f", &p[i].salary);
    }

    // ...
}

You'll then get correct output (example):

Enter informations for person no 1:

Enter name : 
Someone
Enter age : 
104
Enter salary : 
5000
Enter informations for person no 2:

// 2... 3...


Informations for person no 1:

Name: Someone

Age : 104
Salary : 5000.00

// 2... 3...
Rohan Bari
  • 6,770
  • 3
  • 12
  • 31
  • 1
    Please refer to this: [Using fseek with a file pointer that points to stdin](https://stackoverflow.com/q/4917801/10871073) and consider revising your answer. – Adrian Mole Jul 05 '20 at 15:18
  • @AdrianMole it always worked and it doesn't works on Linux terminals. – Rohan Bari Jul 05 '20 at 15:24
  • 1
    Just because it 'works' sometimes doesn't mean that it is good practice. It *does not* conform to the C standard. – Adrian Mole Jul 05 '20 at 15:26
  • @Majkl fgets(p[i].name,100,stdin); This line is a little confusing for me. Can you explain it,please? – Maria Jul 06 '20 at 06:35
  • @Majkl p[i].name[strlen(p[i].name)-1] = '\0'; This line as well. – Maria Jul 06 '20 at 06:41
  • @Maria `p[i].name[strlen(p[i].name) - 1] = '\0'` says that: an element before the last element of the initialized array is assigned to a null which works as a null-terminator. – Rohan Bari Jul 06 '20 at 06:48