Search code examples
cloopsvisual-studio-code

VS Code input needs to be done multiple times


I use a loop to determine if input is correct and if wrong to ask again till input is valid the programm does run but I have to input the number multiple times. If I do run the code on another machine, it runs smothly with just the first input. Here's an example but it is the same, no matter what loop I choose.

  while (input != 1 || getchar() != '\n') {
        while ((c = getchar()) != '\n' && c != EOF) { }
        
        input = scanf ("%d", &number);
        if (input != 1 || getchar() != '\n') {
            printf ("Try again!\n");
        }
    }

Changed the structure of the code and changed the loop conditions and the loop-body, but still multiple times input or one run.

#include <stdio.h>

int main() {

char c;
int input = 0, loopvariable = 1, number, secondtolastfibo = 1, lastbuttwofibo = 1;
long lastfibo;


printf ("Fibonacci-Numbers\n"
        "How many Fibonacci do you want? ");

while (input != 1 || getchar() != '\n') {
    while ((c = getchar()) != '\n' && c != EOF) { }
    
    input = scanf ("%d", &number);
    if (input != 1 || getchar() != '\n') {
    printf ("Try a positive number!\n");
    }
}

secondtolastfibo = 1;
lastbuttwofibo = 1;
printf ("%d. \t%d\n", loopvariable, secondtolastfibo);
loopvariable++;
printf ("%d. \t%d\n", loopvariable, secondtolastfibo);
while (loopvariable < number) {
    
    fflush(stdin);
    loopvariable++;
    lastfibo = secondtolastfibo + lastbuttwofibo;
    lastbuttwofibo = secondtolastfibo;
    secondtolastfibo = lastfibo;

    printf ("%d. \t%ld\n", loopvariable, lastfibo);
    
    if (loopvariable % 10 == 0) {
        printf ("Continue with Return.......");
        getchar();
    }
}


return 0;

}

In- and Output: Fibonacci-Numbers How many Fibonacci do you want? 23\n 23\n

  1.  1
    
  2.  1
    
  3.  2
    

...

Changed the loop conditions: got rid of "while ((c = getchar()) != '\n' && c != EOF)" or changed the loop condition to (input !=1) or put input = scanf(...) outside the loop and the loop condition to (input !=1) but it didn't work and still had to input multiple times for it to start.


Solution

  • The scanset %1[\n] will scan for one character that must be a newline. The scan will append a zero to the output so char newline[2] is the minimum required. %*[^\n] will scan and discard everything that is not a newline.

    #include <stdio.h>
    
    int main ( void) {
        char newline[2] = "";
        int input = 2, loopvariable = 1, number = 0, secondtolastfibo = 1, lastbuttwofibo = 1;
        long lastfibo = 0;
    
    
        printf ("Fibonacci-Numbers\n"
        "How many Fibonacci do you want? ");
    
        do {
            if ( input != 2) {
                scanf ( "%*[^\n]"); // scan and discard everything not a newline
                printf ( "\tTry again\n");
            }
            input = scanf ("%d%1[\n]", &number, newline); // scan for integer and a newline
            if (input == EOF) {
                fprintf ( stderr, "EOF\n");
                return 1;
            }
        } while (input != 2);
    
        secondtolastfibo = 1;
        lastbuttwofibo = 1;
        printf ("%d. \t%d\n", loopvariable, secondtolastfibo);
        loopvariable++;
        printf ("%d. \t%d\n", loopvariable, secondtolastfibo);
        while (loopvariable < number) {
    
            loopvariable++;
            lastfibo = secondtolastfibo + lastbuttwofibo;
            lastbuttwofibo = secondtolastfibo;
            secondtolastfibo = lastfibo;
    
            printf ("%d. \t%ld\n", loopvariable, lastfibo);
    
            if (loopvariable % 10 == 0) {
                do {
                    printf ("Continue with Return.......");
                    scanf ( "%*[^\n]"); // scan and discard everything not a newline
                    input = scanf ( "%1[\n]", newline); // scan for one character that muse be a newline
                    if (input == EOF) {
                        fprintf ( stderr, "EOF\n");
                        return 1;
                    }
                } while (input != 1);
            }
        }
    
    
        return 0;
    }