S
Sandy Beech
Hi
I am doing an exercise involving time in C, the task is to check a users
age against his date-of-birth. However after displaying the prompt for
date-of-birth, the program skips immediately to the output without
leaving any chance for the user to input a date of birth.
I have tried putting fflush(stdin) before the prompt, but that didnt work
either, can anyone help.
#include<stdio.h>
#include<time.h>
void main()
{
char name[80];
int age;
char dob[80];
struct tm dob_tm;
time_t now=time(0);
printf("what is you're name:\n");
gets(name);
printf("what is you're age:\n");
scanf("%d", &age);
printf("enter you're date of birth (mm/dd/yy):\n");
fflush(stdin);
gets(dob);
strptime(dob,"%D",&dob_tm);
printf("hello %s,",name);
if(localtime(&now)->tm_year-dob_tm.tm_year!=age)
printf("you lied about you're age, you are %d!!\n"
,localtime(&now)->tm_year-dob_tm.tm_year);
}
I am doing an exercise involving time in C, the task is to check a users
age against his date-of-birth. However after displaying the prompt for
date-of-birth, the program skips immediately to the output without
leaving any chance for the user to input a date of birth.
I have tried putting fflush(stdin) before the prompt, but that didnt work
either, can anyone help.
#include<stdio.h>
#include<time.h>
void main()
{
char name[80];
int age;
char dob[80];
struct tm dob_tm;
time_t now=time(0);
printf("what is you're name:\n");
gets(name);
printf("what is you're age:\n");
scanf("%d", &age);
printf("enter you're date of birth (mm/dd/yy):\n");
fflush(stdin);
gets(dob);
strptime(dob,"%D",&dob_tm);
printf("hello %s,",name);
if(localtime(&now)->tm_year-dob_tm.tm_year!=age)
printf("you lied about you're age, you are %d!!\n"
,localtime(&now)->tm_year-dob_tm.tm_year);
}