[C] Buggy Printing aid

Short version: I wrote a program using C. A while loop rejects invalid input. Chaining bad input the right way creates a printf infinite loop.


Long version

Hello! After using my laser printer with Libreoffice under Linux I found that there was a speed penalty when printing on both sides of the paper using the all-odd, reverse-all-even method.
I had to write the page numbers one by one. This software improves that to two pairs of copy-paste.

I always try to think in ways to catch unexpected input/behaviour. Ironically that’s the part that’s preventing everything else from working without issues.

The first part of my program makes sure that the user has provided a positive number as input, discarding letters and asking again for input in case of negatives or zero. The problem is, providing a negative and then characters are a super combo finish, printing the intended warnings :+1: forever after :-1: unless halted.

Thanks for reading all this.
Do you know what’s wrong with my program?

AFAICT what’s happening here is that an incorrect character is caught in the buffer.

When your code reaches the scanf the second time, it’s still seeing the previously incorrect char. You can flush the buffer with something hacky and untested like:

while((int c = getchar()) != '\n' && c != EOF);

Hacky and untested, I like that. I’ll give it a try!