2013-11-22 3 views
-4

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(); 
    } 
} 
+0

btw가 너무 커서 코드를 들여 보내십시오. 그러면 읽을 수 있고 해당 블록을 볼 수 있습니다! –

답변

0

흠 먼저 큰 프로그램 (너무)이을보고 내 symetric 검출기는 subanagram()에서보고 :

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

을 메인에는 다음과 같습니다

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

및 귀하의 질문 :

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

입력 한 문자열 인(개행 문자)은 fgets(stinrg, 100, stdin)의 경우에 포함됩니다. scanf("%s", string)과는 다릅니다. 따라서 종료 문자열이 char quit[] = "\\q\n"으로 변경되거나 마지막으로 \n'을 삭제하면 치료와 같은 것으로 판단됩니다.

관련 문제