0
#include <stdio.h>

void input(void);
void avgSalary(void);

struct Person{
    char name[15];
    int age;
    float salary;
};

struct Person person[10];

int main(){
    int choice, avg;
    char cont;
    
    do{
        printf("1. Input person record \n2. Average salary\n");
        printf("\nPlease enter selection: ");
        scanf("%d", &choice);
        if (choice == 1){
            input();
        }
        else if (choice == 2){
            avgSalary();
        }
        else{
            printf("Error");
        };
        printf("\nContinue?: ");
        scanf("%c", &cont);
        printf("\n");       
    }while(toupper(cont)=='Y');
}

void input(){
    int x;
    getchar();
    for (x=0;x<10;x++){
        printf("Person %d\n", x+1);
        printf("Name: ");
        gets(person[x].name);
        printf("Age: ");
        scanf("%d", &person[x].age);
        printf("Salary: ");
        scanf("%f", &person[x].salary);
    };
}

void avgSalary(){
    int x, sum=0;
    float avg=0;
    for (x=0;x<10;x++){
        sum += person[x].salary;
    };
    avg = sum/10;
    printf("The average salary of %d person is %.2f\n", x, avg);
}

For the output, It asks for person's info and another is the average salary. We select 1 then after entering the first person's name, age and salary, I couldn't enter the next person's name all the way until the 10th person. Why is this happening?

Yyaann
  • 11
  • 3
  • it still doesn't work. I mean like when i wanted to enter the second person name it skipped and show name: age: – Yyaann Apr 12 '22 at 15:34
  • 1
    The newline is not being consumed after call `scanf` on some of your calls, causing execution flow to _skip_ over some of your code. `scanf("%c", &cont);` -> `scanf(" %c", &cont);` (adding a space before `%c` removes any white space (blanks, tabs, or newlines)) – ryyker Apr 12 '22 at 15:34
  • _"It still doesn't work"_ is not very helpful. – ryyker Apr 12 '22 at 15:34
  • Run the code in debugger with break points to see what is happening. Then update your post with more specific information. – ryyker Apr 12 '22 at 15:45
  • I would recommend replacing all `scanf()`, and `gets()` calls with `fgets(buf, sizeof buf, stding);`. For strings, replace `buf` with corresponding string struct member. For numbers, use `buf` as argument to either `strtol()` or `strtod()` calls to convert to number. – ryyker Apr 12 '22 at 15:50

1 Answers1

-1

Try replacing gets(person[x].name); with scanf .You could try scanf("%s",person[x].name); . Or you could probably (not recommended) add a getchar() at the end of your loop,after the last scanf call. In your first run through the loop,you are getting the behavior you want because you use the getchar() that is being called before the loop.

Edit:

Keep in mind that,using scanf you can't take an input containing a full name. If you want to do that,you can either use a seperate array for the last name in your Struct.Or else you can just probably get away with using getchar() and gets.

Paner
  • 20
  • 4
  • That `scanf` will fail to read a name with spaces in it, reading only the first part of the name. – hyde Apr 12 '22 at 16:33
  • Allocating that little space made me believe he was only trying to keep only the first name,without the surname. Probably a more suitable approach is to keep a seperate array in the struct containing the surname. But ,if both first name and lastname is needed, ```getchar()``` could be used as mentioned. @hyde – Paner Apr 12 '22 at 17:22