Total Anime Immersion
Total Anime Immersion

Reputation: 471

Why does the else part auto trigger on the second loop in my program?

I have a block of code like the following :

char option;

while (1) {
    option = -1;
    option = getch();

    if(option == 13){
    //do something
    }

    else{
    //do something
    }

}

On the second loop, the else part auto triggers, even if I press enter(13), for some reason and hence I'm unable to understand why this is happening?

Upvotes: 2

Views: 653

Answers (1)

Arthur
Arthur

Reputation: 11

Essencially the getch function only reads the first thing you press in your keyboard or already is in the buffer. So, if you try to type 13, the getch function will only read the '1'.

Upvotes: 1

Related Questions