1

I wrote this C program to enter names and ages of 3 people. But the output wasn't my expectation. It was able to enter name and age for the first person, but it wasn't able for second and third persons. Please help.

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

int main()
{
    int i, age;
    char name[20];

    for(i=0; i<3; i++)
    {
        printf("\nEnter name: ");
        gets(name);

        printf("Enter age: ");
        scanf(" %d", &age);

        puts(name);
        printf(" %d", age);
    }

    return 0;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • You should check the return value from `scanf()`. On your second loop, if you typed a name, that name would probably not be converted by `%d` and would be left in the input to be read the next time around the loop — while leaving you puzzled about what's going on. See [Why the `gets()` function is to dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/) for why you should not use `gets()` and what alternatives are available. You should test that the replacement (which is likely to be `fgets()`) was successful. Always check input operations! – Jonathan Leffler Oct 02 '18 at 05:44
  • Thank you for your suggestion. – user10444184 Oct 02 '18 at 16:54

2 Answers2

1

In short: Your 2nd puts is processing the '\n' from your scanf.

Fix by adding getchar(); after scanf

Explanation:

1st iteration:

    printf("\nEnter name: ");
    gets(name);                     // line is read from input
    printf("Enter age: ");
    scanf(" %d", &age);             // a number is read from input, and the newline char ('\n') remains in buffer
    puts(name);
    printf(" %d", age);

2nd iteration:

    printf("\nEnter name: ");
    gets(name);                     // previously buffered newline char is read, thus "skipping" user input
    printf("Enter age: ");
    scanf(" %d", &age);             
    puts(name);
    printf(" %d", age);

Same goes for 3rd iteration, and this is why you lose user input

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

The best way to store information of more than one person is to use struct, like

  struct person {
      int age;
      char name[20];
  };

and make array of struct, like

 struct person people[3];

than use loop with accessing people[i].age and people[i].name, e.g.:

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

struct person {
    int age;
    char name[20];
};

#define ARR_SIZE 3

int main(int argc, char* argv[])
{
    struct person people[ARR_SIZE];
    int i;
    char *lastpos;
    for(i = 0; i < ARR_SIZE; i++)
    {
        printf("\nEnter name: ");
        scanf(" %s", people[i].name);
        if ((lastpos=strchr(people[i].name, '\n')) != NULL) *lastpos = '\0'; // remove newline from the end
        printf("Enter age: ");
        scanf(" %d", &people[i].age);
    }
    printf("This is the people you entered:\n");    
    for(i = 0; i < ARR_SIZE; i++)
    {
        printf("%d : %s : %d\n", i+1, people[i].name, people[i].age);
    }
    return 0;
}

UPDATE:

As you see I use scanf(" %s", people[i].name); instead of gets(people[i].name); to read name from stdin. Try both option for the following cases:

  • enter short name (e.g. John) and correct age (e.g. 15)
  • enter two word name (e.g. John Smith) and correct age (e.g. 17)
  • enter short name and uncorrect age (e.g. five)

Then read articles about value returned by scanf and cleaning input buffer

VolAnd
  • 6,367
  • 3
  • 25
  • 43