I'm trying to do a program with a simple game for a user to guess the number. My code is below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 30
#define TRYING 5
void guessnumber(int, int, int *);
int main(void) {
    int mytry = 1;
    guessnumber(MAX, TRYING, &mytry);
    if (mytry <= TRYING)
        printf("Congratulations! You got it right in %d tries\n", mytry);
    else
        printf("Unfortunately you could not guess the number in the number of tries predefined\n");
    printf("End\n");
    return EXIT_SUCCESS;
}
void guessnumber(int _n, int _m, int *_mytry) {
    srandom(time(NULL));
    int generated = 0, mynum = 0, test = 0;
    generated = rand() % (_n + 1);
    printf("Welcome to \"Guess the number\" \n");
    printf("A number between 0 and %d was generated\n", _n);
    printf("Guess the number:\n");
    while (*_mytry <= TRYING) {
        test = scanf(" %d", &mynum);
        if (test != 1 || mynum < 0 || mynum > MAX)
            printf("ERROR: please enter a valid number \n");
        else
        if (mynum > generated)
            printf("Wrong! The number your trying to guess is smaller\n");
        else
        if (mynum < generated)
            printf("Wrong ! The number your trying to guess is bigger\n");
        else
            break;
        *_mytry = *_mytry + 1;
    }
}
Okay, now the program is working pretty ok except for one thing: the scanf test. 
It works if I try to enter a number out of my range (negative or above my upper limit) but it fails if I for example try to enter a letter. What it does is that it prints the message of error _m times and then it prints  "Unfortunately you could not guess the number in the number of tries predefined" and "End".
What am I doing wrong and how can I fix this?
 
     
    