I've made a simple program in Turbo C which acts like a guessing game. It should end the game after the user inputs 3 wrong characters, or as soon as the correct one is guessed. However it doesn't work and I can't figure out why. The game just closes after I input 4 characters (wrong or not).
#include <stdio.h>
#include <conio.h>
void main(void)
{
char ch;
int tries = 0;
int win_flag = 0;
clrscr();
printf("Guess the letter: ");
do
{
ch = getch();
printf("%c", ch);
tries++;
if (ch == 'a')
win_flag = 1;
else
win_flag = 0;
}
while (tries <= 3 || win_flag == 1);
if (tries <= 3)
printf("\nGame over.");
else if (win_flag == 1)
printf("\nYou win!");
getch();
}
Change your loop condition as
while (tries <= 3 && win_flag == 0);