简体   繁体   English

Scanf 在 const char 数组中不超过 4 个字符 - C

[英]Scanf doesnt take more than 4 characters in a const char array - C

I have a really weird bug.我有一个非常奇怪的错误。 If I write a name which contains more than 4 characters, the printf will only output the first 4 characters.如果我写的名称包含超过 4 个字符,则 printf 将只输出前 4 个字符。

Important note: I am not allowed to use any other library than stdio.h and I am not allowed to use anything else than scanf for input and printf for output .重要提示:我不允许使用任何其他库比stdio.h中,我不允许使用别的比scanf函数输入和printf输出 Moreover I am not allowed to modify the paramater list of the functions and I have to use const char .此外,我不允许修改函数的参数列表,我必须使用const char The code runs on putty via ssh on a unix system.代码在 unix 系统上通过 ssh 在 putty 上运行。

My code and the input/output are below.我的代码和输入/输出如下。 In addition, the while loop has a bug too ._.另外,while 循环也有一个 bug ._。

#include <stdio.h>

int searchCharacters(const char*, char);
int getLength(const char*);

int main() {

    char yesNo;
    int end = 0;
    const char name[] = {""};

    printf("please enter a name: ");
    scanf("%s", name);
    int length = getLength(name);
    printf("\n%s has a length of %i", name, length);
    fflush(stdin);

    while(end != 1) {
        printf("\n\nWould you like to search a character in %s (y / n)?", name);
        scanf(" %c", &yesNo);

        switch(yesNo) {
            case 'y':
                printf("\nPlease enter a character: ");
                char searchingCharacter;
                scanf("%c", &searchingCharacter);
                int numberOfCharacters = searchCharacter(name, searchingCharacter);
                printf("\nThe letter %c is %i-times", searchingCharacter, numberOfCharacters);
                break;
            case 'n':
                printf("\nGood bye!");
                end++;
                break;
        }
    }
    return 0;
}

int searchCharacter(const char s[], char c) {
    int numberOfIterations = getLength(s);
    int numberOfCharacters = 0;
    int i;

    for (int i = 0; i < numberOfIterations; i++) {
        if (s[i] == c) {
            numberOfCharacters++;
        }
    }
    return numberOfCharacters;
}

int getLength(const char s[]) {
    int i = 0;
    while(s[i++]);

    return (i - 1);
}

Input/Output:
    please enter a name: abcdefg

    abcd has a length of 7 characters.

    Would you like to search a character in abcd (y / n)? y

<-------------- AUTOMATIC/BUG WHILE LOOP --------------------------->

    Please enter a character:
    The letter
      is 0-times.

</--------------AUTOMATIC/BUG WHILE LOOP---------------------------->

    Would you like to search a character in abcd (y / n)? n

    Good bye!

So, here is a possible answer:所以,这是一个可能的答案:

"Change const char name[] to char name[100] " (from @kaylum) “将const char name[]更改为char name[100] ”(来自 @kaylum)

"Change scanf("%c", &searchingCharacter) --> scanf(" %c", %searchingCharacters) to consume new line in input stream" (from @user3121023) “更改scanf("%c", &searchingCharacter) --> scanf(" %c", %searchingCharacters)以在输入流中使用新行”(来自 @user3121023)

Here is the full code:这是完整的代码:

#include <stdio.h>

int searchCharacters(const char*, char);
int getLength(const char*);

int main() {

    char yesNo;
    int end = 0;
    char name[100]; <-- Changed -->

    printf("please enter a name: ");
    scanf("%99s", name);
    fflush(stdin);
    int length = getLength(name);
    printf("\n%s has a length of %i", name, length);

    while(end != 1) {
        printf("\n\nWould you like to search a character in %s (y / n)?", name);
        scanf("%c", &yesNo); <-- Changed -->

        switch(yesNo) {
            case 'y':
                printf("\nPlease enter a character: ");
                char searchingCharacter;
                scanf(" %c", &searchingCharacter); <-- Changed -->
                int numberOfCharacters = searchCharacter(name, searchingCharacter);
                printf("\nThe letter %c is %i-times", searchingCharacter, 
                numberOfCharacters);
                break;
            case 'n':
                printf("\nGood bye!");
                end++;
                break;
        }
    }
    return 0;
}

int searchCharacter(const char s[], char c) {
    int numberOfIterations = getLength(s);
    int numberOfCharacters = 0;
    int i;

    for (int i = 0; i < numberOfIterations; i++) {
        if (s[i] == c) {
            numberOfCharacters++;
        }
    }
    return numberOfCharacters;
}

int getLength(const char s[]) {
    int i = 0;
    while(s[i++]);

    return (i - 1);
}

Input/Output:
    please enter a name: abcdefg

    abcdefg has a length of 7 characters. <-- Working/Changed -->

    Would you like to search a character in abcd (y / n)? y

    Please enter a character: a <-- Working/Changed -->

    The letter a is 1-times. <-- Working/Changed -->

    Would you like to search a character in abcdefg (y / n)? n

    Good bye!

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

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