I need to scan for the following input type using C:
number letter number

so i used the follwing:

scanf("%d%c%d", &n1, &lett, &n2);

Unfortunately with this, whenever I attempt to input a single number and press enter, scanf continues to expect another input. How can I adjust the program so that the user must enter the correct format first go.

I tried:

if (scanf("%d%c%d", &n1, &lett, &n2) == 1) {
} else {
printf("Please enter correct format");

but didnt work

Thanks, new to programming

Read the entire input in one line with fgets() . Then check the input to make sure it is in the correct format. If not, do what you need to do. If so, convert the character digits to numbers for n1 & n2 and move the letter to lett

Isn't there anyway to make it work with scanf?
If not, then I'll look into fgets


Not easily. scanf() is notoriously ill-mannered.

lol. If you don't mind, can you point me in the right direction as to how I would do it with scanf? I would really like to know..


Use %1s if you want to read a single letter (and make the result char aLetter[2]).

%c takes the next character without exception.