char again;
do {
counter = 0;
while (counter < 3) {
printf("Please enter a number: ");
scanf("%d", &num);
counter++;
sum += num;
}
if (counter == 3) {
printf("Would you like to continue? [Y]Yes [N]No:");
scanf("%c", &again);
}
}while (again == 'Y');
I can’t seem to figure out why this won’t work. If i enter Y it breaks, if I enter N it breaks. I need to loop until the user enters “N” to exit the program and no other letter.
Change the
scanfcall to:The trick is in the space before the
%c: it instructs thescanffunction to ignore any whitespace character before returning your N or Y. Otherwise you will be reading the return carriages from the previousscanfcalls.From
man 3p scanf(the POSIX one):That is a complex function. I recommend reading the man page with care: it can do much more than most people think.