c - 不确定为什么程序不终止

标签 c string loops

如果你往下看main函数,错误就出在第一个输入处。 当我输入\q 时,我的程序并没有像我希望的那样终止。

谁能向我解释一下为什么会发生这种情况?

我注意到,如果我将 fgets 更改为 scanf,则\q 部分工作得很好,但是(使用 scanf)如果我输入带有任何空格的字符串,我的输入将在空格后终止。

#include <stdio.h>
#include <cstring>
#include <ctype.h>


int count(char *input, char c) {
    int k = strlen(input); 
    return 0; 
}



void subanagram() {
    char yes[50] = "yes"; 
    char no [50] = "no"; 
    char play[100]; 
    char sa[100]; 
    char strin[100];
    char quitt[75] = { '/', 'q', '\0'}; 

    do {
    printf("Would you like to play the sub-anagram game?"); 

    //scanf("%s", play); 
    fgets(play, 100, stdin); 

    if(stricmp(play, yes) == 0) {
        printf("Enter a potential sub-anagram (or /q to quit): "); 
        scanf("%s", sa); 

        if(stricmp(sa, quitt) == 0) {
            break; 
        }

    }




}
while (stricmp(yes, play) == 0 || stricmp(no, play) == 0); 
}

void main() {
    char string[100];
    char pally[75];
    char nospace[100];
    char reversed[100]; 
    char yes[75] = {'y', 'e', 's', '\0'}; 
    char quit[75] = { '\\', 'q', '\0'}; 
    char no[75] = {'n', 'o', '\0'}; 
    char play[50]; 
    int p, i, k, j=0; 

    printf("Enter a word or phrase (or \\q to quit): "); 
    fgets(string, 100, stdin);  
    //scanf("%s", string);

    k = strlen(string); 

    if(stricmp(quit, string) == 0) {
        printf("Thank you for using my program!\n"); 
        return; 
    }

    do {
      printf("Would you like to see if the phrase is a palindrome?"); 
      scanf("%s", pally); 
    } while (stricmp(yes, pally) == 1 || stricmp(no, pally) == 1); 

    if(stricmp(yes, pally) == 0) {


        for(i=0; i<k; i++) {
            if(!isspace(string[i]) == 1) {

                nospace[j] = string[i];
                nospace[j+1] = '\0'; 
                j++;

        }
    }
                strcpy(reversed, nospace); 
                strrev(reversed); 

                if(stricmp(nospace, reversed) == 0) {
                    printf("The phrase %s IS a palindrome.\n", string);
                    subanagram(); 
                }

                else { printf("The phrase %s IS NOT a palindrome.\n", string); 
                    subanagram(); 
                }


    }
        if(stricmp(pally, no) == 0) {
    /*  printf("Would you like to play the sub-anagram game?");
        scanf("%s", play); 
        if(strcmp(play, yes) == 0) {
            subanagram(); 
        }*/
        subanagram();  
    } 
}

最佳答案

嗯,第一次查看这个(太大了)大型程序时,我的对称检测器在 subanagram() 中看到:

{ '/', 'q',
"...(or /q to quit): "

主要是:

char quit[75] = { '\\', 'q',

你的问题是:

"When I type \q, my program does not"

关于c - 不确定为什么程序不终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139133/

相关文章:

c - 使用 ncurses 打印字符失败

c - 对 'specificFunction' 的 undefined reference

c - GNU cpp 是否奇怪地为零参数的宏解释 C99 标准?

C 代码,使用 NULL 与 0,最佳实践

C动态字符串长度

loops - 从 Golang 中的 slice 中删除

string - 为什么我们不使用前缀树(trie)来查找最长公共(public)子串?

java - 删除 CSV 文档中字段开头和结尾的引用

C++ "OR"条件被忽略

linux - 如何告诉ffmpeg按顺序遍历目录中的所有文件