c++
  • c
  • windows
  • loops
  • 2015-09-20 115 views -1 likes 
    -1

    我是C編程語言的新手。我有一個問題,我怎樣才能結束循環在Windows中。EOF和GETCHAR結束循環

    #include<stdio.h> 
    #include<conio.h> 
    
    int main() { 
        printf("enter ur palindrome"); 
        int i[100], c, d = 0, n = 0; 
    
        c = getchar(); 
        while (c != '\n') { 
         i[d] = c; 
         d++; 
        } 
    loop: 
        while (d != 0) { 
         if ((i[0 + n] != i[d - n])) { 
          n++; 
          goto loop; 
         } 
    
         printf("this is not a palindrome"); 
    
         break; 
        } 
        printf("this is a palindrome"); 
    
        return (0); 
    } 
    

    我已經試過幾乎所有CTRL + Z,CTRL + C,CTRL + d,替換 '\ n' WITH EOF ,還有更多的事情。沒有爲我工作。我在Windows 10上使用CodeBlocks。 是否有其他方式來編寫除getchar和eof以外的其他類型的程序。

    +0

    使用。 –

    +0

    @ ameyCU while循環不終止 –

    +0

    @ Atomic_alarm if + retrun怎麼辦?可以üplzz解釋 –

    回答

    0

    而不是使用goto不同的方式,使用continue;來重複循環。

    但是,發佈代碼還存在其他一些問題。

    The array `int i[100]` is never terminated with a NUL byte ('\0') 
    An array of char not int should be used. 
    this loop: `while (d != 0) will never exit, 
        because (if the loop is ever entered) 
        the `d` variable is never changed within the loop 
    

    這裏是我的推薦代碼:

    警告:如果+回報不徹底的測試

    #include <stdio.h> 
    //#include<conio.h> <-- not used, so do not include 
    #include <string.h> 
    
    #define MAX_LENGTH (100) 
    
    int main(void) 
    { 
        int d; 
        int n; 
        char i[MAX_LENGTH]; 
    
        printf("enter ur palindrome\n"); // <-- \n so will immediately print 
    
        if (NULL != fgets(i, MAX_LENGTH, stdin)) 
        { 
         if(strlen("\n") < strlen(i)) 
         { // then, some string entered 
    
          // remove any trailing '\n' 
          char *newline = strstr(i, "\n"); 
          if(newline) 
          { // then '\n' found 
           *newline = '\0'; 
          } // end if 
    
          d = strlen(i); 
          for(n=0; (d-n) >= n; n++) 
          { 
    
           if(i[0 + n] != i[d - n]) 
           { // then no match 
            printf("this is not a palindrome"); 
            break; 
           } // end if 
          } // end for 
    
          if((d-n) < n) 
          { // then palindrome 
           printf("this is a palindrome"); 
          } // end if 
         } 
    
         else 
         { 
          printf("nothing entered\n"); 
         } // end if 
        } // end if 
    
        return (0); 
    } // end function: main 
    
    0

    你可能想看看在本節再次

    c=getchar(); 
        while(c!= '\n') // Value of c never altered, hence it'll never break 
        { i[d]=c; 
        d++; 
        } 
    

    ,是的,其他環

    loop: // not required 
        while ( d!=0 ) // can't see change in d within this body, is it n ? 
        { 
        if((i[0+n] != i[d-n])) 
        { 
         n++; 
         goto loop; // not required 
         continue; //will do 
        } 
        printf("this is not a palindrome"); 
        break; 
    } 
    

    和你實際上得到一個額外的消息說

    this is a palindrome 
    

    印刷後

    this is a not palindrome 
    

    我想這不是你想要的。

    +0

    ameycu我是新來的:) :) –

    +0

    @azamiftikhar是的,我很感謝你的努力,但我的觀點是在編碼之前仔細考慮算法,這樣編碼後你將不得不做更少的修改。對不起,如果聽起來很刺耳,我不是故意的。 – ameyCU

    +0

    感謝ameycu和kkk對你寶貴的意見 –

    0

    在這種循環中,您需要再次讀取下一個字符因此需要在循環

    c=getchar(); 
        while(c!= '\n') 
        { 
        i[d]=c; 
        d++; 
        c=getchar(); 
        } 
    
    0

    這裏補充getchar()是編寫一段代碼

    #include <stdio.h> 
    #include <string.h> 
    
    
    int main() 
    { 
        char *word; 
        int i; 
        int n; 
    
        printf("enter ur palindrome"); 
        scanf("%[^\n]", word);      // reads input all the way to the new line 
        n = strlen(word); 
    
        for (i = 0; i < n; i++) { 
         if (word[ i ] != word[ n-1 - i ]) { 
          break; /* for */ 
         } 
        } 
        if (i == n) { 
         printf("This is a palindrome"); 
        } else { 
         printf("This is not a palindrome"); 
        } 
    
        return 0; 
    } 
    
    +0

    1)'word'沒有字符內存 - 這是一個未初始化的指針! 2)'scanf(「%[^ \ n]」,word);'如果輸入以'\ n''開頭,'不能將任何東西掃描到'word'中。 – chux

    相關問題