Program doesn't wait for user input with scanf(“%c”,&yn);

独自空忆成欢 提交于 2019-11-26 10:35:56
Alok Save
printf("Please enter an output filename: ");    
scanf("%s",&outfilename);

When you enter the second string and hit the ENTER key, a string and a character are placed in the input buffer, they are namely: the entered string and the newline character.The string gets consumed by the scanf but the newline remains in the input buffer.

Further,

scanf("%c",&yn);

Your next scanf for reading the character just reads/consumes the newline and hence never waits for user input.

Solution is to consume the extra newline by using:

scanf(" %c", &yn);
      ^^^   <------------Note the space

Or by using getchar()

You may want to check out my answer here for a detailed step by step explanation of the problem.

Use

scanf("%20s",&filename);

and remember that stdin is line buffered and on Linux is following a tty discipline

You could use GNU readline or ncurses if you want more detailed control.

scanf("%s", ...) leaves the \n terminating the line in the input. It isn't causing a problem for the next one as scanf("%s", ...) starts by skipping whites. scanf("%c", ...) doesn't and thus you read the \n.

BTW You'll probably meet other problems is you put spaces in your file name (%s doesn't read them) and if you enter too long names (%s has no input length limitations).

One solution for the problem you complained (but not the other one) is to use scanf(" %c", ...) (see the space before %c? scanf is tricky to use) which starts by skipping white spaces.

scanf("%s",&filename);

also remove the &

scanf.c:13: warning: format '%s' expects type 'char ', but argument 2 has type 'char ()[20u]'

The better way to handle this problem I found is explained here.

It recomends to use an alternative way of handle input and is very well explained.

I use always this function to get user input.


char * read_line (char * buf, size_t length) {
    /**** Copyright de home.datacomm.ch/t_wolf/tw/c/getting_input.html#skip
    Read at most 'length'-1 characters from the file 'f' into
    'buf' and zero-terminate this character sequence. If the
    line contains more characters, discard the rest.
    */
    char *p;
    if ((p = fgets (buf, length, stdin))) {
        size_t last = strlen (buf) - 1;
        if (buf[last] == '\n') {
            /**** Discard the trailing newline */
            buf[last] = '\0';
        } else {
            /**** There's no newline in the buffer, therefore there must be
            more characters on that line: discard them!
            */
            fscanf (stdin, "%*[^\n]");
            /**** And also discard the newline... */
            (void) fgetc (stdin);
        } /* end if */
    } /* end if */
    return p;
} /* end read_line */

Old Answer

I fixed this sort of problems with this rule:

// first I get what I want.
c = getchar();
// but after any user input I clear the input buffer
// until the \n character:
while (getchar() != '\n');
// this also discard any extra (unexpected) character.

If you make this after any input, there should be not problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!