0

I am a starter and this is my original program:

#include <stdio.h>
int main(void){
    char c;
    int n,m,i,j,w;

    scanf("%c %d %d",&c,&m,&n);
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            printf("%c",c);
        }
        printf("\n");
    }
    return 0;
}

I want to repeat the input, so I add a while loop:

#include <stdio.h>
int main(void){
    char c,d;
    int n,m,i,j,w;

    while(1){
        scanf("%c %d %d",&c,&m,&n);
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                printf("%c",c);
            }
            printf("\n");
        }
    }
    return 0;
}

When I run the program, the first input and output is normal, but it has an empty region between the second input and second output and so on, and I don't know why, and how can I correct my program?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kevin
  • 59
  • 6
  • 4
    [You may want to read this](https://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c). There are *hundreds* of duplicates for this question, but alas the titles and question bodies are usually so divergent actually searching for them can be a task in itself. The short answer is: you're not accounting for the newline character at the end of each input line, and your `%c` is consuming it. – WhozCraig Feb 19 '17 at 04:31
  • 1
    `while` is a loop, or a keyword (or a statement), but is not a function. – Jonathan Leffler Feb 19 '17 at 04:35
  • A statement, even... – autistic Feb 19 '17 at 04:40
  • @WhozCraig If only they'd all read [the `fscanf` manual](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html), rather than guessing how `fscanf` works, we'd probably have a significant reduction in that number... *or* they could *search for `[scanf]` before asking*! – autistic Feb 19 '17 at 04:42
  • 1
    @Seb: even easier: they could echo (print) the values that they read after reading them, and that would help them see the problem. `printf("c = [%c], n = %d, m = %d\n", c, n, m);` and all would be revealed. – Jonathan Leffler Feb 19 '17 at 04:43
  • @JonathanLeffler That would be a great step in forming an MCVE, specifically the *minimalisation* and *verification* parts. – autistic Feb 19 '17 at 04:46
  • [The program doesn't stop on scanf(“%c”, &ch) line, why?](https://stackoverflow.com/questions/20306659/the-program-doesnt-stop-on-scanfc-ch-line-why). – WhozCraig Feb 19 '17 at 05:01

0 Answers0