#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void buffer();
int fib(int x, int y) {
if (y <= 1) {
return x;
} else {
return fib(x, y - 1) + fib(x, y - 2);
}
}
int main(int argc, char * argv[]) {
int x;
int y;
char answer = 'y';
while (answer == 'y') {
printf("Please enter the initial value of the green curd: ");
scanf("%d", &x);
buffer();
while (x < 1) {
printf("I'm sorry that value is unrecognized or is negative.\n");
printf("Please enter the initial value of the green curd: ");
scanf("%d", &x);
buffer();
}
printf("Please enter the number of days: ");
scanf("%d", &y);
buffer();
while (y < 1) {
printf("I'm sorry that value is unrecognized or is negative.\n");
printf("Please enter the number of days: ");
scanf("%d", &y);
buffer();
}
printf("With the initial population of %d pounds of crud growing for %d days.\n", x, y);
printf("The final population would be %d pounds.\n", fib(x, y / 5));
printf("Would you like to continue? (y/n): ");
scanf("%c", &answer);
buffer();
while (answer != 'y') {
if (answer == 'n') {
exit(1);
}
printf("I'm sorry that value is unrecognized or is negative.\n");
printf("Would you like to continue? (y/n): ");
scanf("%c", &answer);
buffer();
}
}
return 0
}
void buffer(void) {
char ch;
scanf("%c", &ch);
while (ch != '\n') {
scanf("%c", &ch);
}
}
I have a project due for school. just looking for some advice. The program runs fine, but when I decide to continue, buffer statement doesn't stop me from entering characters the second time around. It's only when I reach the end to continue, where the program actually uses my while loop to stop any invalid input.