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;
}
You print the address of your integer, not the value. To print the value, use *userChoice.
printf("%d", *userChoice);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With