I have to take two inputs from the user with %
input example: 20% 30%
I tried this
scanf("%d%d ", &x,&y);
how can I input two values with %? I can only take two integer values.
CodePudding user response:
%%
matches literal % character, so this should do it:
int r = scanf("%d%%%d%%", &x,&y);
if(r != 2) {
puts("scanf error");
exit(1);
}
General note: it's more robust to read entire lines with fgets
or POSIX getline
, then parse them, for example with sscanf
.
Also, please read scanf
documentation on what %d
and %%
do, and what the return value actually means.
CodePudding user response:
Firstly, are you really obligated to also take the %? Couldn't you instead ask the user to input integers?
If you still want to take inputs with %, I think you could treat your inputs as char*, iterate through them until you encounter the '%' char, and then delete it. You could then use the atoi function to transform your char* into integers.