Hi,
I need your help for my program.
My code is :
void user()
{
char str[15];
int i=0;
do{
printf("\nPrint str.\n");
fgets(str,15,stdin);
i = strlen(str);
}while(i>15);
}
void menu()
{
printf("Press :\n");
printf("0. \n");
printf("1. \n");
printf("2. \n");
}
int get_choice()
{
int choice = 0 ;
const char* const valid_choices = "012" ;
do
{
fputs( "Your choice : ", stdout ) ;
fflush( stdin ) ;
choice = toupper( fgetc( stdin ) ) ;
fgetc( stdin ) ;
if( strchr(valid_choices, choice) == NULL )
{
fputs("Invalid option.\n");
choice = 0 ;
}
} while( choice == 0 ) ;
return choice ;
}
main()
{
int choice=0;
do
{
menu() ;
choice = get_choice() ;
switch(choice)
{
case 0: user();
break ;
case 1: break ;
case 2: break;
}
}while(choice != 2);
}
When I press string with <15 number of characters all is ok.
But if my str is 17 characters , then is printed two times
'Your choice : Invalid Option'
Could anyone help me fix it ?
Thanks