0
#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.

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
  • `scanf("%c", &answer);` ==> `scanf(" %c", &answer);` (Add space) Tip: read up on `scanf()` - there is a lot to it. – chux - Reinstate Monica Apr 19 '18 at 04:14
  • What do you think happens to the `Enter` keys you input after each number? If you use a debugger to step through the code line by line and check the contents of `answer` after that `scanf` call you would know what the problem was. – Some programmer dude Apr 19 '18 at 04:15
  • New to programming, first semester. I was using the debugger in VB, just couldn't figure out the issue for some reason. Thanks for the advice. – Angel Figueroa Apr 19 '18 at 04:25

1 Answers1

1
scanf("%d", &y);

will leave the newline char in in the input buffer, which is then immediately picked up by the following

scanf("%c", &answer);

You should have this scanf() skip over whitespace, which includes newline chars, by inserting a space before the format string

scanf(" %c", &answer);
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31