0

i create a really simple coding and it got no errors but when it run, i cant put input in the 'age' side.

#include <stdio.h>
#include <conio.h>


struct baby
{
    char name[2][30];
    char sex[2][7];
    char birthday[2][12];
};

struct parents
{
    char nama[2][30];
    int age[2];
};

struct momdad
{
    struct parents father;
    struct parents mother;
};

struct momdad info;
struct baby newborn;

int main()
{
    int i;

    for(i=0;i<2;i++)
    {
        printf("\nEnter baby's name %d: ",i+1);
        gets(newborn.name[i]);

        printf("Enter baby's sex %d (Female/Male): ",i+1);
        gets(newborn.sex[i]);

        printf("Enter baby's birthday %d (dd/mm/yyyy): ",i+1);
        gets(newborn.birthday[i]);

        printf("Enter father's name %d: ",i+1);
        gets(info.father.nama[i]);
        printf("Enter father's age %d: ",i+1);
        gets(info.father.age[i]);

        printf("Enter mother's name %d: ",i+1);
        gets(info.mother.nama[i]);
        printf("Enter mother's age %d: ",i+1);
        gets(info.mother.age[i]);



    }

    printf("\n\n\tNEW BORN BABY IN KUANTAN HOSPITAL");
    printf("\n\n===============================================");

    for(i=0;i<2;i++)
 {


        printf("\n\nBaby name: %s",newborn.name[i]);
        printf("\nSex: %s",newborn.sex[i]);
        printf("\nBirthday: %s",newborn.birthday[i]);
        printf("\n\nFather name: %s",info.father.nama[i]);
        printf("\nFather age: %s",info.father.age[i]);
        printf("\n\nMother name: %s",info.mother.nama[i]);
        printf("\nMother age: %s",info.mother.age[i]);
        printf("\n\n----------------------------------------------");
 }

    getch();
}

this is my declaration that i think is wrong but i dont know how.

int age[2];

and the input will be putting in here

printf("Enter father's age %d: ",i+1);
gets(info.father.age[i]);

n in here

printf("Enter mother's age %d: ",i+1);
gets(info.mother.age[i]);

i'm still new in programming sorry for asking this simple question

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Cheryl Perry
  • 119
  • 1
  • 3
  • 9

1 Answers1

0

Never use gets(). It cannot be used safely, and as of 2011 it's been removed from the language.

In a comment, you mention calling fflush(stdin);. The behavior of fflush is undefined for input streams. Some implementations define the behavior, but depending on that will make your program non-portable -- and you don't need it anyway.

The simplest way to read input data is to use scanf(), but that has some of its own problems. For example, if you use scanf("%d", &n); and type 123, it will consume the 123 and leave anything following it (such as a newline) waiting to be read.

A better way to read input is to use fgets to read a line of text, then sscanf to parse the data from the input line. It re

Here's an example:

#define MAX_LEN 200
char line[MAX_LEN];
int num;

printf("Enter an integer: ");
fflush(stdout);

if (fgets(line, MAX_LEN, stdin) == NULL) {
    fprintf(stderr, "Error reading line\n");
    exit(EXIT_FAILURE);
}
if (sscanf(line, "%d", &num) != 1) {
    fprintf(stderr, "Error parsing integer from line\n");
    exit(EXIT_FAILURE);
}
printf("The number is %d\n", num);

I call fflush(stdout) after the first printf to ensure that the prompt actually appears. stdout can be line-buffered, meaning that output won't appear until you've printed an entire line. The fflush isn't always necessary, but it's a good idea.

The fgets call reads a full line of input or MAX_LEN characters if the line is longer than that. (gets has no way to specify the maximum input size, so no matter how big your target array is, it can always read more and clobber random memory.) fgets returns a null pointer if there was a problem, and I check for that.

sscanf is similar to scanf, but it reads data from a string in memory, not from standard input. (There's also fscanf, which reads from a specified file.) It returns the number of items that it successfully scanned, so a value other than 1 would indicate an error.

I suggest reading the documentation for all these functions; I haven't covered everything they do. In fact, you should read the documentation for any standard library function you use.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631