help please this doesnt work properly
input
omayma.firstname : AAAAAAAAAAAAAAAAAAAAAAAAAAA
omayma.lastname : BBBBBBBBBBBBBBBBBBBBBBBBBBBB
output :
omayma.firstname : AAAAAAAAAABBBBBBBBBB
omayma.lastname : BBBBBBBBBB
expected output :
omayma.firstname : AAAAA (10 A exatcly)
omayma.lastname : BBBBBB (10)
typedef struct
{
char firstname[10];
char lastname[10];
} person;
int main()
{
person omayma;
printf("firstname : ");
scanf("%10s", omayma.firstname);
fflush(stdin);
printf("lastname : ");
scanf("%10s", omayma.lastname);
fflush(stdin);
puts(omayma.firstname);
puts(omayma.lastname);
return 0;
}
Suggestions to get your code to work as you probably expected it to.
First, provide more space for names. space is cheap. Go big (enough) in struct:
typedef struct
{
char firstname[50];
char lastname[50];
} person;
Second, if you have to use scanf()
, make the follow on adjustments for larger buffers...
scanf("%49s", omayma.firstname);// adds room for long names,
Or, you can get rid of scanf()
altogether with what may be a better alternative:
fgets(omayma.firstname, sizeof omayma.firstname, stdin);
omayma.firstname[strcspn(omayma.firstname, "\n")] = 0;//remove newline
fgets(omayma.lastname, sizeof omayma.lastname, stdin);
omayma.lastname[strcspn(omayma.lastname, "\n")] = 0;//remove newline
printf("%s %s", omayma.lastname, omayma.lastname);
Third, fflush() is only used for output streams:
fflush(stdin);//delete this statement (everywhere)