Ale
Hallan people..
I have something like this:
void main(){
int flag=1,exit=1;
cla_N *head=NULL;
char class_name[NAME_LENGTH],choice[1];
double diversity;
while (exit)
{
printf("Please choose one of the options below:\n"
"1- Create/update class. \n"
"2- Delete class. \n"
"3- Insert new species. \n"
"4- Delete species. \n"
"5- Update species. \n"
"6- Print species. \n"
"7- Print class. \n"
"8- Exit. \n");
//scanf("%d", &flag);
gets(choice);
//choice=getchar();
//scanf("%c",&choice[0]);
switch (choice[0])
{
case '1':
printf("Enter class name:\n");
//gets(class_name);
gets(class_name);
printf("Enter diversity:\n");
scanf("%lf", &diversity);
head=func1(head,class_name,diversity);
break;
case '7':
printf("Enter class name:\n");
//gets(class_name);
gets(class_name);
func7(head,class_name);
break;
case '8':
exit=0;
break;
default:
printf("Error: input incorrect! Please try again.\n");
break;
}
}
}
When i choose for the first time it's okay.....after i'm done with my first action it prints the MENU (it's good) and autimatically prints the default option(error:...bla bla) and the menu again.....Means it's skips the 'gets'.....WHY? Any ideas? i tried the 'scanf', 'getchar', everything....what is the right way to check input to be valid? AND ONCE AND FOR ALL! CAN SOMEONE TELL ME WHAT'S WRONG WITH THE 'GETS' FUNC? IT TOTALLY DOES IT'S OWN THING AND I DON'T KNOW WHAT IT IS.