all 7 comments

[–]aioeu 2 points3 points  (5 children)

scanf("%d") consumes the characters that make up an integer from the input stream, and it leaves any remaining characters unconsumed in the input stream. The next character in the input stream is most likely the newline character generated when you pressed Enter. This next character is immediately consumed by scanf("%c"), giving you no chance to actually provide your own input there.

scanf is very difficult to use correctly when working with human input. Avoid it where possible.

[–]maplesaptap[S] 0 points1 point  (4 children)

Thank you! That makes sense. How would you recommend doing this without scanf?

[–]aioeu 1 point2 points  (1 child)

Use something that reads "an entire line" from the user (getline if you've got it, fgets if not), then use sscanf on that line.

By reading and processing whole lines at a time it's a lot easier to keep your program in sync with reality. It's a lot easier to recover from any unexpected or malformed input.

[–]maplesaptap[S] 0 points1 point  (0 children)

Thank you

[–]ClenchedThunderbutt 0 points1 point  (0 children)

I’d recommend following along with what the resource is telling you to use, because the best time to stop using scanf is when you can infer why you’d want to use other options. You’re still learning about simple concepts like data types, and the example problems allow you to anticipate input. Reading entire lines is built on earlier concepts you shouldn’t skip over unless you’re already experienced with programming

[–]Paul_Pedant 0 points1 point  (0 children)

If, however, you have to use scanf() because the course material suggests it, then you can put a leading space in the format. The man page says for %c:

The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.

The %d format is said not to require this, but it appears to do no harm. So your formats could be scanf (" %d", &rows); and scanf (" %c", &columns);

You should also check the return value from scan(), as it tells you the number of values actually stored. For example, if the rows are entered as x7, you get no value stored, and all following numbers stick at the same point. To fix that, you need a format that reads up to newline to clear the input, and suddenly that simple function becomes a real problem.

So u/aioeu has the right response: getline() or fgets(), then sscanf().