For some reason this while loop executes twice and the strings are printed twice before it prompts for input. I had assumed it was that the input buffer had a character in it, but it does not to my knowledge. Any advice?
while (count >= 0 && stop != 83 && isClosed == 0) {
printf("Percentage of Door Closed: %d\n",count);
count -= 10;
printf("If sensor detects object press 'S'\n");
printf("Press any button to continue closing door\n");
stop = getchar();
if (count == 0) {
isClosed=1;
}
}
Output:
Percentage of Door Closed: 100
If sensor detects object press 'S'
Press any button to continue closing door
Percentage of Door Closed: 90
If sensor detects object press 'S'
Press any button to continue closing door
a
Percentage of Door Closed: 80
If sensor detects object press 'S'
Press any button to continue closing door
Percentage of Door Closed: 70
If sensor detects object press 'S'
Press any button to continue closing door
S
You’re picking up the newline character that gets sent to the input stream when you type
S <Enter>. You can deal with this a couple of ways:or