I have a program in which user input is required, the user enters the number 1-8 to determine how to sort some data, but if the user simply clicks, another function is entered. I get a general idea of how to do this, and I thought that I would be fine, but I am having some problems when the user simply presses the enter key. Currently my code is as follows:
fputs("Enter an option (1-8 or Return): ", stdout);
fflush(stdout);
fgets(input, sizeof input, stdin);
printf("%s entered\n", input);
if(input == "\n")
{
printf("Performing alternate function.");
}
else
{
sscanf(input, "%d", &option);
switch(option)
{
case 1:
printf("Option 1.\n");
break;
case 2:
printf("Option 2.\n");
break;
case 3:
printf("Option 3.\n");
break;
case 4:
printf("Option 4.\n");
break;
case 5:
printf("Option 5.\n");
break;
case 6:
printf("Option 6.\n");
break;
case 7:
printf("Option 7.\n");
break;
case 8:
printf("Option 8.\n");
break;
default:
printf("Error! Invalid option selected!\n");
break;
}
}
Now I changed the if statement to try input == ", input ==" "and input ==" \ n ", but none of them work. Any advice would be greatly appreciated. Currently from what I see , the original if statement will work, and the code will jump to the else part, and then print the default case.
, , , :
char input[2];
int option = 0;