I was asked to write a program that does certain task, which repeats itself if user enters 'y' or ends if user enters 'n' at the end of the task. For the sake of simplicity, let's say the task it to print "have a nice day". Here is my code:
int main(void) {
char cont;
bool repeat = true;
while (repeat == true){
printf("Have a nice day!\n");
printf("continue to print?\n");
scanf("%c",&cont);
if (cont == 'n') {repeat = false;}}
}
However, the output does not seem right. The actual output after entering 'y' twice and then 'n' once is
Have a nice day!
continue to print?
y
Have a nice day!
continue to print?
Have a nice day!
continue to print?
y
Have a nice day!
continue to print?
Have a nice day!
continue to print?
n
As oppose to
Have a nice day!
continue to print?
y
Have a nice day!
continue to print?
y
Have a nice day!
continue to print?
n
So basically each time 'y' is entered, the messages are printed twice istead of once. Any idea where I did wrong?