2016-03-04 116 views
-1

我写了一个关于字符串输入和在字符串中查找元音的代码。一切正常,除非程序不退出do while循环。在循环C中退出时出错

do{ 
    i = 0; j = 1; 
    str = NULL; 
    input = '\0'; 
    for(l = 0; l < 5; l++) { /* initializing arrays with zeros */ 
     lowercase_vowel[l] = 0; 
     uppercase_vowel[l] = 0; 
    } 
    str = (char*)malloc(sizeof(char)); /* allocating space for a character in str */ 
    printf("\nEnter String : "); 
    while(input != '\n') /* until user press enter */ 
     { 
      input = getc(stdin); /* taking the input */ 
      str = (char*)realloc(str,j*sizeof(char)); /* re-allocate memory for character read to be stored */ 
      str[i] = input; /* store read character */ 
      i++; 
      j++; 
     } 
    str[i]='\0'; /* appending null character to the string */ 

    /*Some calculations here */ 
    printf("\nDo you want to continure? (Y/N)"); 
    scanf("%c", &option); 
}while((option != 'N') || (option != 'n')); 

的问题是,当我给的选项,循环就不会结束,也就会跳过该部分

printf("\nEnter String : "); 

,并根据选项的值只是计算。我真的很感激,如果有人在这个问题

+0

你的代码也有未定义的行为 - 它没有为''\ 0''分配空间 –

回答

1

条件

(option != 'N') || (option != 'n') 

是始终为option任何给定值真正帮助了我。您需要将||(或)替换为&&(和)。