Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

scanf in loop crashes IDE

nakaly

New Coder
Hello World!

I keep getting crashed my IDE when I enter a letter in the scanf instead of a number. I tried to use scanf's return value to check that it succesfully matches the input but it still crashes when I enter a letter.

Thank you in advance for any help.

C:
int getMove(Board_t *self, unsigned int *x, unsigned int *y) {

    //Reads values from keyboard and returns them in x and y.

    int inputsMatched = 0;

    if (!errorsInBoard(self)) {
        do {
            printf("Select a cell [row].[column]: ");
            fflush(0);
            inputsMatched = scanf("%u.%u", x, y);
        }
        while (inputsMatched != 2 || *x >= self->dim1 || *y >= self->dim2);
        return 0;
    }
    else {
        return -1;
    }

}
 
Sure, the IDE gets in a strop when receiving invalid input. Not sure why. Try using a generic string input function like gets() and then use sscanf() to parse the string. This works better for me at least.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom