简体   繁体   English

C:程序跳过下一个输入

[英]C: Program skips next input

My program skips the next input after 1 pass through it. 我的程序在经过1次后跳过下一个输入。 I have read the threads on removing the newline character that fgets has, but nothing that was suggested worked . 我已阅读删除换行符是与fgets有线程,但没有什么 建议的工作 Is there anything that would work with microsoft visual studio? 有什么可以与微软视觉工作室一起使用? The best suggestion was "words[strcspn(words, "\\r\\n")] = 0;" 最好的建议是“单词[strcspn(words,”\\ r \\ n“)] = 0;” and this did not remove the new line, unless I am formatting it incorrectly. 这并没有删除新行,除非我格式错误。 I am not allowed to use the strtok function. 我不允许使用strtok函数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 50
#define STOP "quit\n"

char *copywords(char *dest, const char *source, size_t n);

int main(void) 
{

    char words[50];
    char newwords[50];
    size_t num;

    for (;;) {

        printf("\nType a word, or type 'quit' to quit: ");
        (fgets(words, SIZE, stdin));


        if (strcmp(words, STOP) == 0) {
            printf("Good bye!\n");
            return 0;
        }

        printf("Type the # of chars to copy: ");
        scanf_s("%d", &num);

        copywords(newwords, words, num);
        printf("The word was %s\n", words);
        printf("and the copied word is %s", newwords);


    }
}

char *copywords(char *dest, const char *source, size_t n) {
    size_t i;
    for (i = 0; i < n && source[i] != '\0'; i++) {
        dest[i] = source[i];
    }
    dest[i] = '\0';
    return dest;
}

The problem is that you leave the \\n on the input when you call scanf. 问题是当您调用scanf时将\\ n留在输入上。 ie the user types number [return]. 即用户输入数字 [返回]。 You read the number. 你读了这个号码。 When you loop around and call fgets agains the return is still waiting to be read so thats what fgets gets and it returns immediately. 当你循环并再次调用fgets时,返回仍然等待被读取,以便fgets得到什么并立即返回。

I would probably just call fgets the second time you want to read input as well and then use sscanf to read from the string. 我可能只是在第二次调用fgets时调用fgets,然后使用sscanf从字符串中读取。 ie

printf("Type the # of chars to copy: ");
fgets(buffer, ...)
sscanf(buffer, "%d", ...)

As an aside I would also say to check return values as it is easy for fgets or *scanf to fail. 另外,我还要说检查返回值,因为fgets或* scanf很容易失败。

My program skips the next input after 1 pass through it. 我的程序在经过1次后跳过下一个输入。

If I understand you correctly, the problem is that scanf_s (which I assume is like the C standard's scanf) will read the digits into num, but scanf won't remove the following newline from stdin, and so in the next iteration of the loop fgets will see that newline and behave as if it had seen a blank line. 如果我理解正确,问题是scanf_s(我假设它就像C标准的scanf)会将数字读入num,但scanf不会从stdin中删除以下换行符,因此在循环的下一次迭代中fgets将看到该换行符并且表现得好像看到了空行。 I have usually avoided scanf for this reason and instead read a line into a buffer and then parse it. 我通常因为这个原因而避免使用scanf,而是将一行读入缓冲区然后解析它。 For example: 例如:

char buf[50];
...
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d",&num);

(I'd also recommend adding a whole lot more error checking throughout.) (我还建议在整个过程中添加更多的错误检查。)

Here's a straightforward solution. 这是一个简单的解决方案。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 50
#define STOP "quit\n"

char *copywords(char *dest, const char *source, size_t n);

int main(void) 
{

    char words[50];
    char newwords[50];
    size_t num, run = 0;

    for (;;) {

        printf("\nType a word, or type 'quit' to quit: ");
        if(run)
            getchar();
        (fgets(words, SIZE, stdin));

        if (strcmp(words, STOP) == 0) {
            printf("Good bye!\n");
            return 0;
        }

        printf("Type the # of chars to copy: ");
        scanf("%d", &num);

        copywords(newwords, words, num);
        printf("The word was %s\n", words);
        printf("and the copied word is %s", newwords);
        run = 1;
    }
}

char *copywords(char *dest, const char *source, size_t n) {
    size_t i;
    for (i = 0; i < n && source[i] != '\0'; i++) {
        dest[i] = source[i];
    }
    dest[i] = '\0';
    return dest;
}

Since we know there will be an extra '\\n' character left in the stream due to the scanf, just take it out. 因为我们知道由于scanf会在流中留下额外的'\\ n'字符,所以只需将其取出即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM