0

I have a code. Idea is when I an required to enter a number but I enter a non-numeric character, check the error and require me to enter a working number, or continue or break. But it jumps over the code for not entering a number. This is the code:

int EnterNumber(){
    int number;
    printf("Enter your's number:");
    if(!scanf("%d", &number)){
        puts("Keypress don't number!"); 
        int choose = 0;
        puts("Work:\n\t1. Continue 2. Break \nYour's number will choose:");                 
        while(!scanf("%d", &choose)){   // here it jumped.I cann't enter number!
            switch(choose){
                case 1: 
                    EnterNumber();
                    break;
                case 2:
                    break;
                default:
                    puts("Keyboard don't recognize");
                    break;
            }
        }
    }

    return number;
}

Can anyone help me to fix the error, please?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nghia Bui
  • 33
  • 1

1 Answers1

0

You appear to be implementing recursion in your post. But some of the lines in your code are not needed, and hinder the potential efficiency of a recursive algorithm.

The essentials of a recursive algorithm can be captured in 2 statements:

1) Each recursive call should be on a smaller instance of the same problem, that is, a smaller subproblem.
2) The recursive calls must eventually reach a base case, which is solved without further recursion.
(from here)

Following is an adaptation of your original EnterNumber() function reduced to having only those essentials of a recursive algorithm listed above.

  • reads and tests input character (smaller instance...)
  • leaves when test fails criteria (base case...)

It can be expanded as needed to include the other features you desire in your original post (such as more user input, or notice to user that input was not a digit)

void EnterNumber(char a);

int main(void)
{

    char c;
    int num;

    printf("Enter your number:");
    scanf(" %c", &c);
    //     ^ notice space preceeding format char, 
    //       comsumes newline from previous read and allows next input
    num = isdigit(c);
    EnterNumber(c);
    printf("\nHit any key to exit\n");
    getchar();
    return 0;
}

/// Simple demo of recursion:
/// caches input as long as it is a number
/// outputs cache and exits if alpha char
void EnterNumber(char a)
{
    if(isdigit(a) != 0)
    {
        printf("Enter your number:");
        scanf(" %c", &a);
        //     ^ notice space preceeding format char, 
        //       comsumes newline from previous read and allows next input
        EnterNumber(a);    
    }
    printf("%c\n", a);
}

Regarding space in format string of scanf() function:

Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none). (from here)

ryyker
  • 22,849
  • 3
  • 43
  • 87