Hey guys,
I'm just testing some code via user input. I'm trying to do a simple user input to select from 3 options, then enter a name to be entered into a linked list. I was using scanf but have since discovered obvious buffer issues and issues when mixing scanf with fgets and with using it multiple times in quick succession.
No matter what I enter, input convert will always be '10'
I have tried to so many approaches, including casting user_input_c and assigning it to input_convert, also using strtol to convert into user_convert.
All I want is a nice clean way to enter a selection input and then a name.
Code:
int input_convert;
char user_input_c [3];
char contact_name[24];
do {
printf("\nWelcome to your address book\n\n");
printf("1. Add a node\n");
printf("2. Display all nodes\n");
printf("3. Exit\n\n");
printf("2. Enter your choice:- ");
fgets(user_input_c,sizeof(user_input_c),stdin);
input_convert = user_input_c[1] - '0';
printf("user_input_c = %s", user_input_c);
printf("input_convert = %d", input_convert);
switch(input_convert){
case(10):
printf("\n\nYou have chosen to add a new node. \n\nPlease enter a name...\n");
fgets(contact_name,sizeof(contact_name),stdin);
printf("You have entered %s\n\n", contact_name);
contact_create(contact_name, 0);
break;
case(20):
list_disp();
break;
case(30):
exit(1);
break;
default:
printf("\n\n\t Wrong entry, try again");
}
} while (input_convert != 3);