C++ EOF和GETCHAR到end循环

C++ EOF和GETCHAR到end循环,c++,c,windows,loops,C++,C,Windows,Loops,我是C编程语言的新手。我有一个问题,如何在windows中结束循环 #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:

我是C编程语言的新手。我有一个问题,如何在windows中结束循环

#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);
}
#包括
#包括
int main(){
printf(“输入您的回文”);
int i[100],c,d=0,n=0;
c=getchar();
而(c!='\n'){
i[d]=c;
d++;
}
循环:
而(d!=0){
如果((i[0+n]!=i[d-n])){
n++;
后藤环;
}
printf(“这不是回文”);
打破
}
printf(“这是一个回文”);
返回(0);
}
我尝试了几乎所有的方法CTRL+Z、CTRL+C、CTRL+D,用EOF替换“\n” 还有更多的事情。对我来说什么都不管用。我在Windows10上使用代码块。
除了getchar和eof之外,还有其他方法来编写这种类型的程序。

您可能想再看看这一部分

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

我想这不是您想要的。

在该循环中,您需要再次读取下一个字符,因此需要在该循环中添加
getchar()

  c=getchar();
  while(c!= '\n')
  {
  i[d]=c;
  d++;
  c=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;
}
#包括
#包括
int main()
{
字符*字;
int i;
int n;
printf(“输入您的回文”);
scanf(“%[^\n]”,word);//读取输入直到新行
n=strlen(单词);
对于(i=0;i
不要使用
转到
,而是使用
继续
重复循环

然而,发布的代码还有许多其他问题

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
#包括

//#包括@ameyCU while循环不终止@Atomic_alarm how can if+retrun。你会说话吗explain@azamiftikhar是的,我计算过了,因此删除了评论。ameycu我是c的新手:)@Azamiftikar是的,我感谢你的努力,但我的观点是在编码前仔细考虑算法,这样你在编码后需要做的更改就更少了。抱歉,如果听起来很刺耳,我不是故意的。注意:代码会对每对字符进行两次比较。Once就可以了。1)
word
没有字符内存-它是一个未初始化的指针!2)
scanf(“%[^\n]”,word)'\n'
开头,则code>无法将任何内容扫描到
word