0

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) {
...program...
} else {
printf("Please enter correct format");
}

but didnt work

Thanks, new to programming

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Salem
0

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

0

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

Thanks

Edited by Darkest Shine: n/a

0

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..

Thanks

0

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

%c takes the next character without exception.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.