[scanf()输入失败

问题描述 投票:-1回答:1

嘿,我知道当我进入主程序时这会失败。它没有得到从scanf()完全输入。尝试用fgets()替换scanf(),但是我发现的p的scanf()效果不佳。现在,我真的不介意使用fgets(),但我不知道如何获得所需的“ p”(如果没有)通过scanf()。

   #include <stdio.h>
    #include <string.h>
    #define MAX 128

    int my_strcmp(char a[], char b[]) {
        int c = 0;
        while (a[c] == b[c]) { 
            if (a[c] == '\0' || b[c] == '\0') 
                break; 
            c++; 
        }
        if (a[c] == '\0' && b[c] == '\0') 
            return 0;
        else
            return -1;
    }

    int my_strncmp(char a[], char b[], int n) {
        int i = 0;
        while (a[i] == b[i] && i < n) {
            i++;
        }
        if ((a[i] == '\0' && b[i] == '\0') || i == n)
            return 0;
        else
            return -1;
    }

    int my_strchr(char a[], char f) {
        int i;
        for (i = 0; a[i] != '\0'; i++) {
            if (a[i] == f)
                return i;
        }
        return -1;
    }

    int main()
    {
        char str1[MAX], str2[MAX];
        int p, result, n = -1;
        char c = ' ';
        printf("Hello,\nThis program will help you process your strings according to what you are looking for.\n");
        printf("If you'd like to compare two strings, type 1,\n");
        printf("If you'd like to compare two strings, but up the length of compresent that you decide, type 2,\n");
        printf("If you'd like to check if a certain letter is in the string, press 3.\n");
        printf("\n\nPlease, type the number: \n");
        scanf("%d", &p);
        if (p == 1) {
            printf("Enter the first string: \n");
            scanf("%[^\n]s", str1);
            printf("Enter the second string: \n");
            scanf("%[^\n]s", str2);
            result = my_strcmp(str1, str2);
            if (result == 0)
                printf("Your strings match!\n%s %s", str1, str2);
            else
                printf("Your strings are different!\n%s %s", str1, str2);
        }
        if (p == 2) {
            printf("Enter the first string: \n");
            scanf("%[^\n]s", str1);
            printf("Enter the second string: \n");
            scanf("%[^\n]s", str2);
            printf("Enter the amount of chars you'd like to compare in those two strings: \n");
            scanf(" %d", &n);
            result = my_strncmp(str1, str2, n);
            if (result == 0)
                printf("Your strings match!\n");
            else
                printf("Your strings are different!\n");
        }
        if (p == 3) {
            printf("Enter the string: \n");
            scanf("%[^\n]s", str1);
            printf("Enter the letter you'd like to check: \n");
            scanf(" %c", &c);
            result = my_strchr(str1, c);
            if (result != -1)
                printf("Your letter is in the %d spot.\n", result);
            else
                printf("Your letter does not exist in this string.\n");
        }

        return 0;
    }
c string scanf
1个回答
0
投票

我建议使用此功能:

void clean_line() {
   scanf("%*[^\n]");
   scanf("%*c");
}

在您的代码中:

printf("\n\nPlease, type the number: \n");
scanf("%d", &p);
clean_line();

if (p == 1) {
    printf("Enter the first string: \n");
    scanf(" %127[^\n]", str1);
    clean_line();
    printf("Enter the second string: \n");
    scanf(" %127[^\n]", str2);
    clean_line();
    result = my_strcmp(str1, str2);
    if (result == 0)
        printf("Your strings match!\n%s %s", str1, str2);
    else
        printf("Your strings are different!\n%s %s", str1, str2);
}

["%*[^\n]"读取所有不需要的字符并丢弃它们,'\n'除外

["%*c"读取字符'\n'并将其丢弃,清理标准输入

© www.soinside.com 2019 - 2024. All rights reserved.