can someone what's wrong with this code ?
I have written a code which promts the user to enter some information about an animal then stock them within a structured data. but the function read_animal doesnt seem to work well. it skeps this scanf:
Code:
scanf("%c",&an.food)
here is the full code
Code:
#include<stdio.h>
//our animals structure.
typedef struct{
char name[10],cont[10],food,habit;
int leg;
} animal_t;
//decalartion of functions.
animal_t read_animal(void);
void print_animal(animal_t);
//main function of the program
int main()
{
animal_t an;
int c,c2;
printf("Do you want to enter informations about an animal: 1-Yes / 2-No\n");
scanf("%d",&c);
while( c == 1)
{
an = read_animal();
printf("Do you want to see the informations about the animal: 1-Yes / 2-No\n");
scanf("%d",&c2);
if( c2 == 1)
{
print_animal(an);
}
printf("Do you want to enter informations about an animal:1-Yes/2-No\n");
scanf("%d",&c);
}
printf("You have choosen to quit! Bey\n");
}
//functions codes
animal_t read_animal()
{
animal_t an;
printf("Enter the name of the animal:");
scanf("%s",an.name);
printf("\n");
printf("Enter The continent where does the animal live:");
scanf("%s",an.cont);
printf("\n");
printf("Enter how many legs does he have:");
scanf("%d",&an.leg);
printf("\n");
printf("Enter What it eats c = carnivore, h = herbivore, o = omnivore:");
scanf("%c",&an.food);
printf("\n");
printf("Enter its primary habitat:");
scanf("%c",&an.habit);
printf("\n");
return an;
}
void print_animal(animal_t an)
{
printf("The informations about the animal are:\n\n");
printf("_______________________________________________________\n");
printf("name:%s\ncontinent:%s\nlegs:%d\nfood:%c\nhabitat:%c\n",an.name,an.cont,an.leg,an.food,an.habit);
printf("_______________________________________________________\n");
}