Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting char into an int not working

Tags:

c

I know there are multiple ways about this, but I am trying to get the user to input a character and have that character converted into a specific integer for later use. I am using #define constants to simplify things, in case something needs changing later on. The value that I am getting for userChoice is not 0, 1,or 2 but a large number, so something is wrong.

These are the relevant parts of the code.

    #define ROCK 0
    #define PAPER 1
    #define SCISSORS 2
    void getData (int* userChoice)
{
    char charvalue;
    printf("\n\nEnter the R, P, S, or Q (for quit) ");
    scanf("%c", &charvalue);
    charvalue = toupper(charvalue);
    if (charvalue == 'R')
        *userChoice = ROCK;
    else if (charvalue == 'P')
        *userChoice = PAPER;
    else if (charvalue == 'S')
        *userChoice = SCISSORS;
    else if (charvalue == 'Q')
        exit (1);
    else
        printf("\nerror");
    printf("%d", userChoice);

    return;

}
like image 416
0x41414141 Avatar asked Dec 07 '25 19:12

0x41414141


1 Answers

You print the address of your integer, not the value. To print the value, use *userChoice.

printf("%d", *userChoice);
like image 67
poitroae Avatar answered Dec 09 '25 17:12

poitroae