开发者

C - Reading user input

开发者 https://www.devze.com 2022-12-21 08:42 出处:网络
I have a program where user input is required, a user types in a number 1-8 to determine how to sort some data, but if the user just hits enter a different function is performed.I get the general idea

I have a program where user input is required, a user types in a number 1-8 to determine how to sort some data, but if the user just hits enter a different function is performed. I get the general idea of how to do this and I thought what I had would work just fine but I'm having some issues when it comes to when the user just hits the enter key. Currently my code looks as follows:

//User input needed for sorting.    
fputs("Enter an 开发者_如何学Pythonoption (1-8 or Return): ", stdout);
fflush(stdout);
fgets(input, sizeof input, stdin);

printf("%s entered\n", input);  //DEBUGGING PURPOSES

//If no option was entered:
if(input == "\n")
{
    printf("Performing alternate function.");
}
//An option was entered.
else
{
    //Convert input string to an integer value to compare in switch statment.
    sscanf(input, "%d", &option);

    //Determine how data will be sorted based on option entered.
    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've changed the if statement to try input == "", input == " ", and input == "\n" but none of these seems to work. Any advice would be greatly appreciated. Currently from what I can see, the initial if statement fails and the code jumps to the else portion and then prints the default case.

Just to be clear the variables I declared for this code are as follows:

char input[2];          //Used to read user input.
int option = 0;         //Convert user input to an integer (Used in switch statement).  


The problem is in how you're doing the string comparison (if (input == "\n")). C doesn't have a "native" string type, so to compare strings, you need to use strcmp() instead of ==. Alternatively, you could just compare to the first character of the input: if (input[0] == '\n') .... Since you're then comparing char's instead of strings, the comparison doesn't require a function.


Try:

#include <string.h>

at the top and

if(strcmp(input, "\n") == 0)

in place if your if ( input == ... )

Basically, you have to use string comparison functions in C, you can't use comparison operators.


Try:

input[0] == '\n'

(or *input == '\n')


You need to use single quotes rather than double quotes

if(input == "\n")

compares the input address to the address of the string "\n",

What you want to do is to compare the first character of the input buffer to the character literal \n like this

if(input[0] == '\n')

Note the use of single quotes around '\n'


You need to capture return code from sscanf, it will tell you how many of the field are "assigned", which in "Enter" key case, return code of 0

edit: you should use strcmp when comparing string, not the operator "==".


The issue is with strings, you are comparing pointers, i.e. memory addresses. Since the input and "\n" aren't the same exact memory, it always fails (I assume input is a char *). Since you're looking for a single character, you can instead dereference input and compare to a char using single quotes instead of double.

(*input == '\n')

Should work as you intend.

0

精彩评论

暂无评论...
验证码 换一张
取 消