0

I want to use scanf to get two integers separated by a comma from the user.
So the scanf is in a loop that checks if the user really gave two integers or just some random strings, and if he did gave strings the program prints an error message and loop again. (if the user gave two integers the loop breaks)

so this is my code:

unsigned int x, y;
printf("Please enter your coordinates: ");
while (true){
    fflush(stdin);
    if (scanf("%d, %d", &x, &y) == 2){
        printf("%d, %d\n", x, y);
        break;
    } else {
        printf("Not a valid format! please try again: "); // error message
    }
}

But when I run this the program just prints the error message endlessly.
From what I understand the program should wait again for another user input in the scanf (which is also why I did fflush, so it will clean the stdin buffer)

I really don't know what's going on, so any answer will help!
Thank you

Edit: I know that there are better ways to do it that scanf, but unfortunately I have to do it this way

neta cohen
  • 137
  • 11
  • `scanf` is no good for this sort of thing. It leaves the unrecognizable characters on the input stream, where they cause endless trouble. – Steve Summit Nov 16 '22 at 21:49
  • 1
    `fflush(stdin)` is a popular way to *try* to solve this problem, but it's not guaranteed to work. – Steve Summit Nov 16 '22 at 21:50
  • See [this question](https://stackoverflow.com/questions/2979209) and [this question](https://stackoverflow.com/questions/34219549). Also [this question](https://stackoverflow.com/questions/7898215). – Steve Summit Nov 16 '22 at 21:51
  • @SteveSummit and it is UB --`— The stream for the fflush function points to an input stream or to an update stream in which the most recent operation was input (7.19.5.2).` – 0___________ Nov 16 '22 at 21:52
  • @SteveSummit add to the dupe links – 0___________ Nov 16 '22 at 21:53
  • @SteveSummit: Though it's perfectly reasonable to follow it up by just consuming from `stdin` until you hit a newline to discard the assumed garbage line and reprompting for a new line. – ShadowRanger Nov 16 '22 at 21:54
  • @ShadowRanger I disagree that's "perfectly reasonable". `scanf`'s sole virtue is simplicity. If, after every time you call it and it fails, you have to consume from `stdin` until you hit a newline to discard the garbage, that's not simple, and (IMO) it's not reasonable. – Steve Summit Nov 16 '22 at 21:56
  • I never saw an example reading more than one value with scanf. Here is an [example link telling about troubles](https://stackoverflow.com/questions/15740024/why-does-scanf-ask-twice-for-input-when-theres-a-newline-at-the-end-of-the-form).I would read one after the other, and "printf" what should be entered first. That would make it work in a simple manner, ask for e. g. x coordinate, `scanf("%d", &x)`, ask for y coordinate, `scanf("%d", &y)`, ... - or read a string with `scanf("%s", x_and_y_as_comma_separated_text)` and parse it yourself, split at comma, validate, handle syntax errors etc.... – BitLauncher Nov 16 '22 at 22:00
  • As usual, I suggest _less guessing and more reading_ (such as [these FAQs](https://c-faq.com/stdio/stdinflush.html) (**all of them; do it now**) and K&R2e (do the exercises as you stumble across them). – autistic Nov 16 '22 at 22:14

0 Answers0